Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用';使用system.Reflection访问基类成员;路径';_C#_Oop_System.reflection - Fatal编程技术网

C# 使用';使用system.Reflection访问基类成员;路径';

C# 使用';使用system.Reflection访问基类成员;路径';,c#,oop,system.reflection,C#,Oop,System.reflection,所以我有这样的想法: using System.Reflection; public class SomeFieldsGrouped{ public int foo; public string bar; //<etc> } public class A{ public SomeFieldsGrouped someFieldsGroupedInA; //<etc> } public class B : A{ publi

所以我有这样的想法:

using System.Reflection;

public class SomeFieldsGrouped{
    public int foo;
    public string bar;
    //<etc>
}

public class A{
    public SomeFieldsGrouped someFieldsGroupedInA; 
    //<etc>
}
public class B : A{
    public int ffo;
    //<etc>
}

public class YetAnotherClass
{
    public B b; //Instantiated and stuff
    public string bbr = "someFieldsGroupedInA.foo";

    public static object GetPropertyValue (object o, string path)
        {
            object value = o;
            string[] pathComponents = path.Split (new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
            if (pathComponents.Length == 1) {
                return value.GetType ().GetField (pathComponents [0]).GetValue (o);
            }
            foreach (var component in pathComponents) {
                value = value.GetType ().GetProperty (component).GetValue (value, null);//this is where it stops everytime
            }
        return value;
    }

    public void Main(){
        object o = GetPropertyValue(b, bbr); //where is your god now?
    }
}
public static object GetPropertyValue (object o, string path)
{
    object value = o;
    string[] pathComponents = path.Split (new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
    if (pathComponents.Length == 1) {
        return value.GetType().GetProperty(pathComponents [0]).GetValue (o, null);
    }
    foreach (var component in pathComponents) {
        value = value.GetType().GetProperty (component).GetValue (value, null);
    }
    return value;
}
使用系统反射;
公共类SomeFields分组{
公众谘询委员会;;
公共字符串栏;
//
}
公共A类{
公共SomeFields分组SomeFields分组数据;
//
}
B级:A级{
公共场所;
//
}
公共类YetAnotherClass
{
public B;//实例化了
公共字符串bbr=“someFieldsGroupedInA.foo”;
公共静态对象GetPropertyValue(对象o,字符串路径)
{
对象值=o;
string[]pathComponents=path.Split(新字符[]{.'},StringSplitOptions.RemoveEmptyEntries);
如果(pathComponents.Length==1){
返回value.GetType().GetField(pathComponents[0]).GetValue(o);
}
foreach(pathComponents中的var组件){
value=value.GetType().GetProperty(component).GetValue(value,null);//每次都在这里停止
}
返回值;
}
公共图书馆{
objecto=GetPropertyValue(b,bbr);//你的上帝现在在哪里?
}
}
(请记住它不是实际代码)

我试图实现的是基于反射的方法,通过反射,我可以访问路径为字符串的任何属性。现在这里发生了一件有趣的事情,我应该能够访问“someFieldsGroupedInA”,因为它是基类成员。我可以用b.SomeFieldsGroupedInA来做,但为什么我不能用反射呢


如果有人能告诉我大致的方向,我在哪里可以找到像我要写的东西,因为我很难做一些能与数组和其他东西一起工作的东西,我真的很感激。

你的代码有一个问题,就是你在类中定义字段,但是当您试图访问它们时,您使用
GetProperty
而不是
GetField

要修复此问题,请替换此代码(在循环中):

使用此代码:

value = value.GetType().GetField(component).GetValue(value);
如果路径可以指定属性和/或字段,那么您应该进行代码检查,查看是否有具有特定名称的属性。如果找不到,它将检查是否有具有此名称的字段

更妙的是,正如@Silvermind所说,最好使用属性而不是字段。下面是您的类的外观:

public class SomeFieldsGrouped{
    public int foo {get;set;}
    public string bar {get;set;}
    //<etc>
}

public class A{
    public SomeFieldsGrouped someFieldsGroupedInA {get;set;} 
    //<etc>
}

public class B : A{
    public int ffo {get;set;}
    //<etc>
}

您知道问题中的代码是定义字段而不是属性吗?(例如,
A.someFieldsGroupedInA
)?我强烈建议op使用公共属性,而不是使用公共字段。
public static object GetPropertyValue (object o, string path)
{
    object value = o;
    string[] pathComponents = path.Split (new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
    if (pathComponents.Length == 1) {
        return value.GetType().GetProperty(pathComponents [0]).GetValue (o, null);
    }
    foreach (var component in pathComponents) {
        value = value.GetType().GetProperty (component).GetValue (value, null);
    }
    return value;
}