我可以使用反射来获取已通过new关键字覆盖的基本属性吗?

Rac*_*hel 3 c# reflection .net-3.5

我正在使用一些使用自定义属性进行验证的代码.我试图覆盖子类中的基类的验证属性,因此它在我们的通用控件中使用相同的名称进行绑定,但没有验证属性.

public interface IBaseClass
{
    [TestAttribute]
    string Id { get; set; }
}

public interface IChildClass : IBaseClass
{
    new string Id { get; set; }
}

public class BaseClass : IBaseClass
{
    public string Id { get; set; }
}

public class ChildClass : BaseClass, IChildClass
{
    public new string Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我们的验证框架使用反射来获取值时,它返回子值而不是基值.

例如,

  • BaseClass.Id = null
  • ChildClass.Id ="B"

使用PropertyInfofor BaseClass.Id,我收到了来自的值ChildClass.Id.

是否可以使用反射来获取BaseClass.Id而不是ChildClass.Id在查看类型的对象时ChildClass

下面是一些示例代码,您可以将其粘贴到命令行应用程序中以查看我要执行的操作.我添加的评论与我得到的和预期的一样.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Starting");

        ChildClass c = new ChildClass();
        c.Id = "B";

        foreach (Type interfaceType in GetInterfaces<IChildClass>())
        {
            foreach (PropertyInfo p in interfaceType.GetProperties())
            {
                Attribute[] attribs = Attribute.GetCustomAttributes(p, typeof(TestAttribute));
                foreach (TestAttribute v in attribs)
                {
                    if (v != null)
                    {
                        object val = p.GetValue(c, new object[0]);

                        // Put breakpoint here
                        v.Validate(val);

                        // p.ReflectedType is IBaseClass, so I would expect
                        // val to be BaseClass.Id (null), however it is instead
                        // returning me ChildClass.Id ("B")

                        // Can I use reflection to get BaseClass.Id here,
                        // without hardcoding any types?

                    }
                }
            }
        }


        Console.ReadLine();
    }

    private static IList<Type> GetInterfaces<B>()
    {
        List<Type> interfaces = new List<Type>();
        interfaces.Add(typeof(B));

        Type[] values = typeof(B).GetInterfaces();

        foreach (Type t in values)
        {
            interfaces.Add(t);
        }

        return interfaces;
    }
}


public interface IBaseClass
{
    [TestAttribute]
    string Id { get; set; }
}

// for sample purposes, removing inheritance from IBaseClass here
// gets me the desired result, however this is not an option in 
// the application I am working on. This base interface must remain on the 
// child interface
public interface IChildClass : IBaseClass
{
    new string Id { get; set; }
}

public class BaseClass : IBaseClass
{
    public string Id { get; set; }
}

public class ChildClass : BaseClass, IChildClass
{
    public new string Id { get; set; }
}

public class TestAttribute : Attribute
{
    public TestAttribute() { }

    public void Validate(object value)
    {
        if (value == null)
            return;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:问题经历了几次迭代,因为我试图对我正在研究的代码框架进行排序,并重现了我想要做的一个简单示例.我想我终于有了一个很好的工作代码示例,展示了我得到的与我想要的东西.

Eri*_*ert 6

所有这些东西都有反射和属性,还有红鲱鱼.根本问题在于你对接口的工作方式有一些错误的看法.让我大大简化你的程序:

using System;
public interface IBaseClass
{
    string Id { get; set; }
}
public interface IChildClass : IBaseClass
{
    new string Id { get; set; }
}
public class BaseClass : IBaseClass
{
    public string Id { get; set; }
}
public class ChildClass : BaseClass, IChildClass
{
    public new string Id { get; set; }
}
public class Program
{
    static public void Main(string[] args)
    {
        ChildClass c = new ChildClass();
        ((BaseClass)c).Id = "Base";
        c.Id = "Child";
        Console.WriteLine( ((BaseClass)c).Id);    // Base
        Console.WriteLine( ((ChildClass)c).Id);   // Child
        Console.WriteLine( ((IBaseClass)c).Id);   // Child !!!
        Console.WriteLine( ((IChildClass)c).Id);  // Child
    }
}
Run Code Online (Sandbox Code Playgroud)

你的问题是你相信第三行应该说Base,但这是错误的.应该说Child.

当你说class ChildClass : BaseClass, IChildClass你说的时候,请找到我ChildClass所需成员的最佳属性绑定IChildClass.这是什么IChildClass要求?它需要两个名为Id的字符串属性.对两者的最佳绑定是ChildClass.Id.

因此,当您掌握了属性信息IBaseClass.Id并询问价值时,您会得到Child,因为ChildClass那里,这是最好的绑定.

现在让我们更有趣.

using System;
public interface IB
{
    string P { get; }
}
public interface IC : IB
{
    new string P { get;}
}
public class B : IB
{
    public string P => "B";
}
public class C : B, IC
{
    public new string P => "C";
}
public class D1 : C  // NO IC
{
    public new string P => "D1";
}
public class D2 : C, IC // YES IC
{
    public new string P => "D2";
}
public class Program
{
    static public void Main(string[] args)
    {
        var d1 = new D1();
        var d2 = new D2();
        Console.WriteLine(((B)d1).P);
        Console.WriteLine(((C)d1).P);
        Console.WriteLine(((IB)d1).P);
        Console.WriteLine(((IC)d1).P);
        Console.WriteLine(((D1)d1).P);
        Console.WriteLine(((B)d2).P);
        Console.WriteLine(((C)d2).P);
        Console.WriteLine(((IB)d2).P);
        Console.WriteLine(((IC)d2).P);
        Console.WriteLine(((D2)d2).P);
    }
}
Run Code Online (Sandbox Code Playgroud)

你能预测一下这个程序的行为吗?

这是C#最微妙的规则之一: 当您在类声明中明确地放置接口时,该接口的每个成员都会重新绑定到最佳值.

  • @Rachel:这是一个很好的推理,但还有一个更微妙的观点.假设你有'D类:C,IC`和`C`没有*实现`IC`.你当然希望`D`中的`IC.P`指的是`DP`.现在假设您编辑`C`来实现`IC`.您是否期望更改*基类*以更改*派生类*中接口方法的绑定?那将是一个非常奇怪的突破性变化; 这是*脆弱基类*问题的一个版本.C#中许多看似不寻常的设计选择可以缓解脆弱的基类故障. (4认同)