也许我也不知道如何去寻找它,但事实是我找不到任何帮助我的东西。
有没有像assoc数组(字典)这样的从父类调用属性的方法?
示例:
using System;
class Foobar
{
public string bla;
public Foobar()
{
this.bla = "hello world";
}
}
public class Test
{
public static void Main()
{
Foobar x = new Foobar();
Console.WriteLine(x.bla); //this works (prints hello world)
Console.WriteLine(x["bla"]); //this wont work but is my achivment
}
}澄清我想要的是..。
我想要创建一个具有一些属性的类,例如
class SomeClass
{
private string aaa {get;set;};
private string bbb {get;set;};
private string ccc {get;set;};
private string ddd {get;set;};
....
}并通过字典在另一个类中循环这一点。
SomeClass x = new SomeClass();
IDictionary<string, string> dict = new Dictionary<string, string>();
dict["a"] = "aaa";
dict["b"] = "bbb";
dict["d"] = "ddd";
foreach( d in dict )
{
someMethode(x[d]);
}发布于 2017-03-30 12:07:46
用这种方式修改类
class Foobar
{
public string bla { get; set; }
public Foobar()
{
this.bla = "hello world";
}
public string this[string name]
{
get
{
return this.GetType().GetProperty(name).GetValue(this, null).ToString();
}
}
}https://stackoverflow.com/questions/43117291
复制相似问题