Get attribute Werte von Eigentum und Liste Werte, ohne zu wissen, das Attribut type

Möchte ich weitergeben in der Attribut-name und geben den Wert zurück. Diese werden in einem generischen util und wird es nicht wissen, der Typ des Attributs.

Update
Dies ist der eigentliche Arbeits-code, wenn jemand Sie braucht, um dies zu tun. Ich brauchte eine Möglichkeit zu haben, den core-code analysieren die Attribute, ohne zu wissen, was Sie waren.

public void LoadPropertiesToGrid(BaseGridPropertyModel model)
{
    foreach (PropertyInfo prop in ReflectionUtil.FindPublicPropeties(model))
    {
        object editTyp = ReflectionUtil.GetPropertyAttributes(prop, "EditorType"); 
        object rowIdx = ReflectionUtil.GetPropertyAttributes(prop, "ColIndex");
        object name = ReflectionUtil.GetPropertyAttributes(prop, "Name");
        object visible = ReflectionUtil.GetPropertyAttributes(prop, "Visible");
        ConfigureColumn((string) name, (int) rowIdx, (bool) visible, (string) editTyp);
     }
}
[Serializable]
public class CanvasPropertiesViewModel : BaseGridPropertyModel
{
    [PropertiesGrid(Name = "TEsting Name 0", ColIndex = 0)]
    public string StringData1 { get; set; }

    [PropertiesGrid(Name = "TEsting Name 2", ColIndex = 2)]
    public string StringData2 { get; set; }

    [PropertiesGrid(Name = "TEsting Name 1", ColIndex = 1)]
    public string StringData3 { get; set; }
}
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class PropertiesGridAttribute : Attribute
{
    ///<summary>
    ///Editor type
    ///</summary>
    public Type EditorType { get; set; }

    ///<summary>
    ///Sets Column Index
    ///</summary>
    public int ColIndex { get; set; }

    ///<summary>
    ///Visible to Grid
    ///</summary>
    public bool Visible { get; set; }

    ///<summary>
    ///Dispaly Name of the property
    ///</summary>
    public string Name { get; set; }
}
public static object GetPropertyAttributes(PropertyInfo prop, string attributeName)
{
    //look for an attribute that takes one constructor argument
    foreach (CustomAttributeData attribData in prop.GetCustomAttributesData())
    {
        string typeName = attribData.Constructor.DeclaringType.Name;
        //if (attribData.ConstructorArguments.Count == 1 && (typeName == attributeName || typeName == attributeName + "Attribute"))
        //{
        //   return attribData.ConstructorArguments[0].Value;
        //}
        foreach (CustomAttributeNamedArgument att in attribData.NamedArguments)
        {
            if(att.GetPropertyValue<string>("MemberName") == attributeName)
            {
                return att.TypedValue.Value;
            }
        }
    }
    return null;
}

//PropertyExpressionParser 
public static TRet GetPropertyValue<TRet>(this object obj, string propertyPathName)
{
    if (obj == null)
    {
        throw new ArgumentNullException("obj");
    }

    string[] parts = propertyPathName.Split('.');
    string path = propertyPathName;
    object root = obj;

    if (parts.Length > 1)
    {
        path = parts[parts.Length - 1];
        parts = parts.TakeWhile((p, i) => i < parts.Length - 1).ToArray();
        string path2 = String.Join(".", parts);
        root = obj.GetPropertyValue<object>(path2);
    }

    var sourceType = root.GetType();
    var value = (TRet)sourceType.GetProperty(path).GetValue(root, null);
    return value;
}
  • Attribute nicht der Begriff der .Name, .Value. Können Sie ein Beispiel geben, das zeigt ein Codebeispiel und gewünschte Ausgabe?
  • Verwandte: stackoverflow.com/q/6637679
Schreibe einen Kommentar