如何从Obect获取属性值 [英] How to Fetch Attribute Value From Obect

查看:165
本文介绍了如何从Obect获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用条件设计图案



  class  passowrd 
{
[显示(名称= 确认新密码)] < span class =code-comment> // MVC预定义
public 字符串 ConfirmPassword { get ; set ; }

}



如上所述我可能有不同的课程

 < span class =code-keyword> object  a = GetInstance(  1) ; 









此方法将返回密码类的对象



我想从这个a对象中检索DisplayName属性



所以如何检索任何属性属性价值来自对象





我的目标是



动态创建各个型号的视图



所以我想我将从属性获取显示名称和值

并存储在NameValue对中并将其传递给查看

解决方案

请参阅我对该问题的评论。这是你如何获得一个类型的属性:

https://msdn.microsoft.com/en-us/library/dwc6ew1d%28v=vs.110%29.aspx [ ^ ]。



如您所见,还有许多其他方法可以获取属性(请参阅 https://msdn.microsoft.com/en-us/library/system.type%28v=vs.110%29.aspx [ ^ ]),但你需要完全准确这个方法,因为你只需要一个属性类型的属性。该方法将返回属性数组,并且,如果此数组中有元素,则必须使用type-case(使用常规类型转换,而不是动态)它们到指定的属性类型。



对于类型成员的属性,完全相同:你需要具有相同名称的方法,具有相同的签名,执行相同的操作:https://msdn.microsoft.com/en-us/library/dwc6ew1d%28v=vs.110%29.aspx [ ^ ]。



如何找到的实例System.Reflection.MethodInfo 取决于你,但你总是从它的声明类型( System.Type )开始。我只想警告你不要按名字获取方法。您不仅可以使用此名称拥有不同的成员,但这通常是不可支持的:如果您因任何原因更改了成员名称,编译器将不会警告您名称不匹配。比方说,更好的方法是使方法实现某些界面的方法,然后不会涉及按名称查找任何内容。



-SA

I going yo use Conditional design patten

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

}


as like above i may have different classes

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

解决方案

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


这篇关于如何从Obect获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆