【发布时间】:2012-09-15 04:21:42
【问题描述】:
好的,为了澄清,编辑了代码:
问题:如何从BaseClass.TheAttribute...访问TestClassOne/Two 中的属性[MyAttr("...")]?
除 TestClassOne/Two 之外的所有类都将编译到我的“核心”中,并作为开发平台交付给客户。 TestClassOne/Two是客户自己开发的,所以“核心”中不可能有TestClassOne/Two的知识。
下面的代码被编译成“核心”并作为 dll 交付给客户。
[TestMethod()]
public void AttrTest()
{
var one = new TestClassOne();
var attrOne = one.MyTestProperty.TheAttribute;
var two = new TestClassTwo();
var attrTwo = two.MyTestProperty.TheAttribute;
}
public class MyAttr : Attribute
{
private string _test;
public MyAttr(string test)
{
this._test = test;
}
}
public class BaseClass
{
public string TheAttribute
{
get {
// Here I would like to get the "[MyAttr("...")]" from the classes in the bottom
return null;
}
}
}
public class SubClass : BaseClass
{
}
以下代码由客户开发(使用我的 dll)
public class TestClassOne
{
[MyAttr("Attribute one")]
public SubClass MyTestProperty = new SubClass();
}
public class TestClassTwo
{
[MyAttr("Attribute two")]
public SubClass MyTestProperty = new SubClass();
}
【问题讨论】:
-
我在stackoverflow.com/questions/12569139/… 提出了另一个问题。如果这个问题得到解答,你的问题一定会得到解答
标签: c# inheritance attributes custom-attributes