DooDoo
DooDoo

Reputation: 13437

Get Description of attribute based on value of another attribute

Please consider this Code:

[ShowFieldByRole(User1 = false, User2 = false, User3 = true)]
[FieldName(Name = "Desciption1")]
public string G_Sum{set; get;}

[ShowFieldByRole(User1 = false, User2 = false, User3 = false)]
[FieldName(Name = "Desciption2")]
public string G_Sum2{set; get;}

[ShowFieldByRole(User1 = false, User2 = false, User3 = true)]
[FieldName(Name = "Desciption3")]
public string G_Sum3{set; get;}

I create two custom attribute class and now I want get all description(the value of Name property of FieldName attribute) of properties that attribute User3=true with reflection.

for example according to above code I want to get Description2,Descriptio3 because their User3 equal to true

How I can do this?

thanks


Edit 1)

I wrote this code and it returns Description2,Descriptio3:

var My = typeof(ClassWithAttr).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                          .Where(p => p.GetCustomAttributes(typeof(FieldName), true)
                          .Where(ca => ((ShowFieldByRole)ca).User3 == true)
                          .Any()).Select(p=>p.GetCustomAttributes(typeof(FieldName),true).Cast<FieldName>().FirstOrDefault().Name);

Now I want to return [PropertyName , Name] When I write the code this way:

    var My = typeof(ClassWithAttr).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                          .Where(p => p.GetCustomAttributes(typeof(FieldName), true)
                          .Where(ca => ((ShowFieldByRole)ca).User3 == true)
                          .Any()).ToDictionary(f => f.Name, h=>h.GetValue(null).ToString());

but I got this error:

Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type

Where is the problem?

Upvotes: 1

Views: 243

Answers (1)

hazzik
hazzik

Reputation: 13344

In your last example both f and h are PropertyInfo

The correct code for your case is

var dictionay = (from propertyInfo in typeof (ClassWithAttr).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                 where propertyInfo.GetCustomAttributes(typeof (ShowFieldByRoleAttribute), true).Cast<ShowFieldByRoleAttribute>().Any(a => a.User3)
                 from FieldNameAttribute fieldName in propertyInfo.GetCustomAttributes(typeof (FieldNameAttribute), true)
                 select new { PropertyName = propertyInfo.Name, FiledName = fieldName.Name })
    .ToDictionary(x => x.PropertyName, x => x.FiledName);

UPD using method chains

var dictionay = typeof (ClassWithAttr).GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .Where(p => p.GetCustomAttributes(typeof (ShowFieldByRoleAttribute), true).Cast<ShowFieldByRoleAttribute>().Any(a => a.User3))
    .SelectMany(p => p.GetCustomAttributes(typeof (FieldNameAttribute), true).Cast<FieldNameAttribute>(),
                (p, a) => new { PropertyName = p.Name, FiledName = a.Name })
    .ToDictionary(a => a.PropertyName, a => a.FiledName);

Upvotes: 1

Related Questions