如何在Silverlight自定义类中按名称访问类成员 [英] How do I access a class member by it's name in a Silverlight custom class

查看:114
本文介绍了如何在Silverlight自定义类中按名称访问类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好伙伴们



我在Silverlight项目中有一个简单的自定义类。



< pre lang =c#> public class UserClass
{
public string UserID { get ; set ; }
public string Tipo { get ; set ; }
public string WUserName { get ; set ; }
public string NMec { get ; set ; }
public string 区域{ get ; set ; }
public string SubArea { get ; set ; }
public string EquipaOrdem { get ; set ; }
public string Linha { get ; set ; }
public string UserAdm { get ; set ; }
public string SessionId { get ; set ; }

public UserClass()
{}
}





在我的代码中,我已经实现了这个类:

 UserClass newUser =  new  UserClass(); 





在代码逻辑中,我需要设置其中一些成员,但我所知道的是成员名称为字符串。



无论如何设置类成员的值使用它的名称作为某种参数?



类似于:

 newUser(  UserID)= < span class =code-digit> 123 ; 

(我知道这不起作用!)



我一直在谷歌搜索拉斯夫妇时间并且无法找到关于如何做到的单一线索。



谢谢

解决方案

你可以使用Reflection但它会花费大量内存开销加上执行速度较慢。你应该尽可能地避免反思。如果没有其他选择,请尝试使用反射,如下所示。

 PropertyInfo propertyInfo = UserClass.GetType()。GetProperty( 用户ID); 
propertyInfo.SetValue(UserClass,Convert.ChangeType( 123,propertyInfo.PropertyType) , null );





或者你可以改变你的课程如下

  public   class  UserClass 
{

public Dictionary< string,string> settings = new 字典<字符串,字符串>()
{
{ UserID },
{ Tipo },
{ WUserName },
{ NMec },
{ Area },
{ SubArea },
{ EquipaOrdem },
{ UserAdm },
{ SessionId }
};

}



然后你可以获得/设置如下价值

 UserClass user =  new  UserClass(); 
user.settings [ UserID] = test1;
string id = user.settings [ 用户ID];


Hallo fellow coders

I have a simple custom class in a Silverlight project.

public class UserClass
{
    public string UserID { get; set; }
    public string Tipo { get; set; }
    public string WUserName { get; set; }
    public string NMec { get; set; }
    public string Area { get; set; }
    public string SubArea { get; set; }
    public string EquipaOrdem { get; set; }
    public string Linha { get; set; }
    public string UserAdm { get; set; }
    public string SessionId { get; set; }

    public UserClass()
    {}
}



In my code I have instanciated this class:

UserClass newUser = new UserClass();



In the code logic I need to set some of these members but all I know is the member name as a string.

Is there anyway to set the value of a class member using it's name as some sort of parameter?

Something like:

newUser("UserID") = 123;

(I'm aware that this doesn't work!)

I've been googling for the las couple hours and couldn't find a single clue on how to do it.

Thanks

解决方案

you can use Reflection but it will cost large memory overhead plus slower execution. You should avoid Reflection as much as possible. if there is no alternative try below like below by using reflection.

PropertyInfo propertyInfo = UserClass.GetType().GetProperty("UserID");
propertyInfo.SetValue(UserClass, Convert.ChangeType("123", propertyInfo.PropertyType),null);



or you can change your class like below

public class UserClass
{
 
 public Dictionary<string,string> settings = new  Dictionary<string,string>()
		{
			{"UserID" ,""},
			{"Tipo" ,""},
			{"WUserName" ,""},
			{"NMec" ,""},
			{"Area" ,""},
			{"SubArea" ,""},
			{"EquipaOrdem" ,""},
			{"UserAdm" ,""},
			{"SessionId" ,""}
		};

}


then you can get/set values as below

UserClass user= new UserClass();
user.settings["UserID"] ="test1";
string id =user.settings["UserID"];


这篇关于如何在Silverlight自定义类中按名称访问类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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