如何通过字符串来访问类成员在C#中? [英] How to access class member by string in C#?

查看:122
本文介绍了如何通过字符串来访问类成员在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个字串(名称)来访问成员的方式?



例如。如果静态代码是:



  classA.x = someFunction(classB.y); 



但我只有两个字符串:

 字符串x =X; 
字符串Y =Y;



我知道在JavaScript中,你可以简单地做:

  CLASSA [X] = someFunction(ClassB的[Y]); 



但如何做它在C#?



此外,有可能通过字符串定义名称



例如:

 字符串x =XXX; 
类{
布尔X {获取;设置} =>意味着布尔XXX {获取;设置},因为X是一个字符串
}



更新,以tvanfosson,我无法得到它的工作,它是:

 公共类ClassA的
{
公共字符串A {搞定;组; }
}
公共类CLASSB
{
公众诠释B {搞定;组; }
}

变种propertyB = classB.GetType()的getProperty(B)。
VAR propertyA = classA.GetType()的getProperty(A)。
propertyA.SetValue(ClassA的,someFunction(propertyB.GetValue(ClassB的,NULL)作为字符串),NULL);


解决方案

您需要使用的reflection

  VAR propertyB = 。classB.GetType()的getProperty(Y); 
变种propertyA = classA.GetType()的getProperty(x)的。

propertyA.SetValue(ClassA的,someFunction(propertyB.GetValue(ClassB的,NULL)为Foo),NULL);



其中,是参数的类型该 someFunction 要求。请注意,如果 someFunction 需要一个对象你不需要投。如果类型是值类型,那么你就需要使用(美孚)propertyB.GetValue(ClassB的,空)投它来代替。



我假设我们正在与性能,而不是领域工作。如果不是的话,那么你可以改变使用的领域,而不是性能的方法,但你可能要切换到使用属性,而不是作为字段不应通常是公共的。



如果该类型不兼容,也就是说, someFunction 不返回 A 的属性或它的不可转让,那么你就需要做一个转换为适当的类型。同样,如果B的类型与函数的参数不兼容,你需要做同样的事情。

  propetyA.SetValue(ClassA的,someFunction(Convert.ToInt32(propertyB.GetValue(ClassB的,NULL)))的ToString()); 


Is there a way to access member by a string (which is the name)?

E.g. if static code is:

classA.x = someFunction(classB.y);

but I only have two strings:

string x = "x";
string y = "y";

I know in JavaScript you can simply do:

classA[x] = someFunction(classB[y]);

But how to do it in C#?

Also, is it possible to define name by string?

For example:

string x = "xxx";
class{
   bool x {get;set}  => means bool xxx {get;set}, since x is a string
}

UPDATE, to tvanfosson, I cannot get it working, it is:

public class classA
{
    public string A { get; set; }
}
public class classB
{
    public int B { get; set; }
}

var propertyB = classB.GetType().GetProperty("B");
var propertyA = classA.GetType().GetProperty("A");
propertyA.SetValue( classA, someFunction( propertyB.GetValue(classB, null) as string ), null );

解决方案

You need to use reflection.

 var propertyB = classB.GetType().GetProperty(y);
 var propertyA = classA.GetType().GetProperty(x);

 propertyA.SetValue( classA, someFunction( propertyB.GetValue(classB,null) as Foo ), null );

where Foo is the type of the parameter that someFunction requires. Note that if someFunction takes an object you don't need the cast. If the type is a value type then you'll need to use (Foo)propertyB.GetValue(classB,null) to cast it instead.

I'm assuming that we are working with properties, not fields. If that's not the case then you can change to use the methods for fields instead of properties, but you probably should switch to using properties instead as fields shouldn't typically be public.

If the types aren't compatible, i.e., someFunction doesn't return the type of A's property or it's not assignable, then you'll need to do a conversion to the proper type. Similarly if the type of B isn't compatible with the parameter of the function, you'll need to do the same thing.

 propetyA.SetValue( classA, someFunction(Convert.ToInt32( propertyB.GetValue(classB,null))).ToString() );

这篇关于如何通过字符串来访问类成员在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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