刚遇到一个超级难题 现在有三个对象
public class Test
{
public Test()
{
}
private string name;
private Dog dog;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public Dog Dog
{
get { return this.dog; }
set { this.dog = value; }
}
}
public class Dog
{
public Dog()
{ }
private string name;
private Fish fish;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public Fish Fish
{
get { return this.fish; }
set { this.fish = value; }
}
}public class Fish
{
public Fish()
{ }
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}}
//难题来了
正常情况下,我想访问Test中的一个属性 可以用下面的方法
Test test=new Test();
return test.Dog.Fish.Name;
但是 现在我想写个方法GetProperty(strPropertyName),封装test.Dog.Fish.的过程。只要直接输入属性名,比如GetProperty("Fish") ,可以直接返回一个test.Dog.Fish对象
注意:(这里的返回对象必须=test.Dog.Fish)
不知道说清楚了没有,我本来想用个递归,可是失败了,因为这个对象路径不好取
Fish fish = new Fish();
fish.Name = "000";
Dog dog = new Dog();
dog.Name = "1111";
dog.Fish = fish;
Test test = new Test();
test.Name = "222";
test.Dog = dog;
GetPropertyDetail(test,"Dog");
//递归方法
public object GetPropertyDetail(object objParent,string strProperty)
{
System.Type type = test.GetType();
//foreach objParent's mid propertys
foreach (System.Reflection.PropertyInfo pro in type.GetProperties())
{
Type theType = pro.PropertyType;
//if objParent.pro=objProperty
if (pro.Name == strProperty)
return pro;
else if (pro.PropertyType != typeof(String))
return GetPropertyDetail(pro, strProperty);
}
return null;
}
public class Test
{
public Test()
{
}
private string name;
private Dog dog;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public Dog Dog
{
get { return this.dog; }
set { this.dog = value; }
}
}
public class Dog
{
public Dog()
{ }
private string name;
private Fish fish;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public Fish Fish
{
get { return this.fish; }
set { this.fish = value; }
}
}public class Fish
{
public Fish()
{ }
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}}
//难题来了
正常情况下,我想访问Test中的一个属性 可以用下面的方法
Test test=new Test();
return test.Dog.Fish.Name;
但是 现在我想写个方法GetProperty(strPropertyName),封装test.Dog.Fish.的过程。只要直接输入属性名,比如GetProperty("Fish") ,可以直接返回一个test.Dog.Fish对象
注意:(这里的返回对象必须=test.Dog.Fish)
不知道说清楚了没有,我本来想用个递归,可是失败了,因为这个对象路径不好取
Fish fish = new Fish();
fish.Name = "000";
Dog dog = new Dog();
dog.Name = "1111";
dog.Fish = fish;
Test test = new Test();
test.Name = "222";
test.Dog = dog;
GetPropertyDetail(test,"Dog");
//递归方法
public object GetPropertyDetail(object objParent,string strProperty)
{
System.Type type = test.GetType();
//foreach objParent's mid propertys
foreach (System.Reflection.PropertyInfo pro in type.GetProperties())
{
Type theType = pro.PropertyType;
//if objParent.pro=objProperty
if (pro.Name == strProperty)
return pro;
else if (pro.PropertyType != typeof(String))
return GetPropertyDetail(pro, strProperty);
}
return null;
}
{
System.Type type = test.GetType();
System.Reflection.PropertyInfo pro = type.GetProperty(strProperty);
if (pro == null)
{
return null;
}
return pro.GetValue(objParent, null);
}
{
string[] prop = propertyName.Split('.'); object ret = o; for (int i = 0; i < prop.Length; i++)
{
o = DataBinder.Eval(o, prop[i]);
if (o == null)
break;
}
return o;
}偷懒,用了DataBinder.Eval()方法调用:Eval(test,"Dog.Fish");