Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6560073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:24:46+00:00 2026-05-25T13:24:46+00:00

I have a view model which uses custom attributes such as public int Id

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:24:47+00:00Added an answer on May 25, 2026 at 1:24 pm

    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);  
      }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
I have a view model with a collection of other objects in it. public
I have a main page which uses a ViewModel I have created: public class
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I have a problem with Automapper when I try use custom resolver which uses
I have a very simple viewmodel class, and a strongly typed view (which uses
I have an entity model build using EF4.1 code first, which uses a WCF
I have got a masterpage which uses a model @model.topics, I created a partial
I have a ListBox which is populated from a collection of ViewModels, which uses
I have a ViewModel which exposes the string property PageToolBarVisible which can be true

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.