我正在处理一些使用自定义属性进行验证的代码。我正在尝试覆盖子类中基类的经过验证的属性,以便它在我们的通用控件中使用相同的名称进行绑定(bind),但没有验证属性。
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; }
}
我的问题是,当我们的验证框架使用反射获取值时,它返回的是子值而不是基值。
例如,
- BaseClass.Id = null
- ChildClass.Id = "B"
为 BaseClass.Id
使用 PropertyInfo
,我从 ChildClass.Id
接收值。
在查看 ChildClass
类型的对象时,是否可以使用反射获取 BaseClass.Id
而不是 ChildClass.Id
?
这是一些示例代码,您可以将其粘贴到命令行应用程序中以查看我正在尝试执行的操作。我添加了关于我得到的与预期的结果的评论。
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;
}
}
注意:问题已经经历了几次迭代,因为我试图对我正在处理的代码框架进行分类,并重现我正在尝试做的事情的简单示例。我想我终于有了一个很好的工作代码示例,显示了我得到的与我想要的。
最佳答案
所有这些带有反射和属性的东西都是红色鲱鱼。根本问题是您对界面的工作方式有一些错误的看法。让我大大简化您的程序:
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
}
}
你的问题是你认为第三行应该说 Base
,但这是错误的。它应该说 Child
.
当你说 class ChildClass : BaseClass, IChildClass
你是说 请帮我找到 ChildClass
属性的最佳可能绑定(bind)到 IChildClass
的必需成员. 什么是IChildClass
要求?它需要两个都称为 Id 的字符串属性。两者的最佳绑定(bind)是 ChildClass.Id
.
因此,当您手头有 IBaseClass.Id
的属性(property)信息时并要求值(value),你得到 Child
,因为在ChildClass
,这是最好的绑定(bind)方式。
现在让它变得更有趣。
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);
}
}
你能预测这个程序的行为吗?
这是 C# 最微妙的规则之一:当您在类声明中显式放置一个接口(interface)时,该接口(interface)的每个成员都会重新绑定(bind)到最佳值。
关于c# - 我可以使用反射来获取已被 new 关键字覆盖的基本属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45065282/