Click here to Skip to main content
15,895,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I going yo use Conditional design patten

C#
class passowrd
{
  [Display(Name="Confirm new password")]//MVC predefine 
        public string ConfirmPassword { get; set; }

}

as like above i may have different classes
C#
object a=GetInstance("1");




this method will return me object of password class

I want to retrieve DisplayName Attribute from this "a" object

so how to retrieve any properties attribute value from "object"


my aim was

dynamically create Views with respective models

so i was thinking i will fetch Display Name and Value from property
and store in NameValue pair and pass it to View
Posted
Updated 15-Mar-15 3:01am
v4
Comments
Sergey Alexandrovich Kryukov 15-Mar-15 8:52am    
This is metadata, not data. You can get attributes for definitions: types, members.
—SA
Member-515487 15-Mar-15 8:59am    
so ow can i proceed want to create dynamic views base on different model

1 solution

Please see my comment to the question. This is how you can get an attribute for a type:
https://msdn.microsoft.com/en-us/library/dwc6ew1d%28v=vs.110%29.aspx[^].

As you can see, there is a number of other ways to get attributes (see https://msdn.microsoft.com/en-us/library/system.type%28v=vs.110%29.aspx[^]), but you need exactly this method, because you only need the attribute of one attribute type. The method will return you the attribute array, and, if there are elements in this arrays, you will have to type-case (use "regular" type cast, not dynamic) them to the specified attribute type.

Exact same thing goes for attributes of the type members: you need the method of the same name, with the same signature, doing the same: https://msdn.microsoft.com/en-us/library/dwc6ew1d%28v=vs.110%29.aspx[^].

How you find the instance of System.Reflection.MethodInfo is up to you, but you will always start with its declaring type (System.Type). I only want to warn you from getting a method by name. Not only you can have different members with this name, but this is generally unsupportable: if you, by any reason, change the member name, compiler won't warn you about name mismatch. It's much better, say, to make methods implementing the methods of some interface, then finding anything by name won't be involved.

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 15-Mar-15 20:17pm    
+5 for the suggestion of yours.
Sergey Alexandrovich Kryukov 15-Mar-15 20:25pm    
Thank you, Afzaal.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900