2

I have a view model which uses custom attributes such as

public int Id { get; set; }
public string Name { get; set; }
[IsEnumeration(typeof(CaseStatus))]
public string Status { get; set; }

IsEnumeration is a custom attribute which takes an Enumeration superclass as a parameter (actually it takes any type, but that doesn't matter since noone else will be using this)

public class IsEnumerationAttribute : Attribute
{
    public Type Enumeration;
    public IsEnumerationAttribute(Type enumeration)
    {
        Enumeration = enumeration;
    }
}

What I want is to be able to get the type specified for any parameter. Currently my code looks like this:

    public T EnumerationValuesToDisplayNames<T>(T item) where T : new()
    {
        if (LoggedInUser.IsSuper) return item;
        var tProps = typeof (T).GetProperties()
            .Where(prop => Attribute
                 .IsDefined(prop, typeof (IsEnumerationAttribute)));

        foreach (var prop in tProps)
        {
            if (prop.GetValue(item, null) != null)
            {
                /*

    Here I look through all properties with the IsEnumerable attribute.
    I want to do something such as:
                var type = prop.GetAttribute(item, typeof(IsEnumerable));
                var displayName = Enumeration<type>.FromId(prop.GetValue(item, null));
                prop.SetValue(item, displayName);

                */
            }
        }
        return item;
    }

I hope this makes sense, any help would be greatly appreciated, thanks

2
  • Just to add, both the Id and DisplayName for Status are string. Ie a status could be id: CNC, display name: Cancelled. All Enumeration<>.FromValue does is converts between this id and the display name Commented Sep 12, 2011 at 15:13
  • what is the problem? what is not working? Commented Sep 12, 2011 at 15:29

1 Answer 1

2

Assuming from your post you have a class defined as such:

public class Enumeration<T> {

  public static string FromId(string id) {
    // FromId Implmentation
  }

}

Then you should just need

foreach (var prop in tProps) {  
  var id=prop.GetValue(item, null);
  if (id!=null) {  
    var type = prop.GetCustomAttributes(typeof(EnumerationAttribute>,true).OfType<EnumerationAttribute>().Select(x=>x.Enumeration).First();
    var enumerationType=typeof(Enumeration<>).MakeGenericType(type);
    var fromIdMethod=enumerationType.GetMethod("FromId",BindingFlags.Public|BindingFlags.Static|BindingFlags.InvokeMethod);
    var displayName=fromIdMethod.Invoke(null,new object[] {id});
    prop.SetValue(item, displayName);  
  }  
}  

Alternatively you could implement the FromId method directly in the EnumerationAttribute then you could just call it directly like so...

foreach (var prop in tProps) {  
  var id=prop.GetValue(item, null);
  if (id!=null) {  
    var enumAttrib = prop.GetCustomAttributes(typeof(EnumerationAttribute>,true).OfType<EnumerationAttribute>().First();
    var displayName=enumAttrib.FromId((string)id);
    prop.SetValue(item, displayName);  
  }  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.