如何从Obect获取属性值

编程入门 行业动态 更新时间:2024-10-15 20:22:31
本文介绍了如何从Obect获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要使用条件设计图案

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

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

object a = GetInstance( 1) ;

此方法将返回密码类的对象 我想从这个a对象中检索DisplayName属性 所以如何检索任何属性属性价值来自对象 我的目标是 动态创建各个型号的视图 所以我想我将从属性获取显示名称和值 并存储在NameValue对中并将其传递给查看

解决方案

请参阅我对该问题的评论。这是你如何获得一个类型的属性: msdn.microsoft/en-us/library/dwc6ew1d%28v=vs.110%29.aspx [ ^ ]。 如您所见,还有许多其他方法可以获取属性(请参阅 msdn.microsoft/en-us/library/system.type%28v=vs.110%29.aspx [ ^ ]),但你需要完全准确这个方法,因为你只需要一个属性类型的属性。该方法将返回属性数组,并且,如果此数组中有元素,则必须使用type-case(使用常规类型转换,而不是动态)它们到指定的属性类型。 对于类型成员的属性,完全相同:你需要具有相同名称的方法,具有相同的签名,执行相同的操作:msdn.microsoft/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: msdn.microsoft/en-us/library/dwc6ew1d%28v=vs.110%29.aspx[^]. As you can see, there is a number of other ways to get attributes (see msdn.microsoft/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: msdn.microsoft/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获取属性值

本文发布于:2023-11-07 00:40:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   Obect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!