English 中文(简体)
利用思考来获得静态财产价值,对衍生物和基类进行分类
原标题:Using reflection to get static property value, a concatenation of derived and base class

我将竭尽全力在这里解释我的愿景。 这是一个非常突出的例子。 缩略语 Bag s and they all hold their own special category of Marble. 每种类型的<代码>Marble <>/code>都有自己的一套尼克名称(string)。

不幸的是,除了马布莱人外,还有其他东西,大通人在这里赢得帮助。

// Bags
abstract class Bag {
    protected Type MarbleType { get; }
    protected List<Marble> _marbles;

    public void DumpBag()
    { ... }
}
class RedBag : Bag {
    override Type MarbleType { get { return typeof(RedMarble); } }
}
class BlueBag : Bag {
    override Type MarbleType { get { return typeof(BlueMarble); } }
}

// Marbles
abstract class Marble {
    public static IEnumerable<string> Nicknames {
        get {
            return new List<string>() {
               "Marble", "RollyThing"
            }
        }
    }
}
class RedMarble : Marble {
    public static IEnumerable<string> Nicknames {
        get {
            return new List<string>(Marble.Nicknames) {
                "Ruby"
            };
        }
    }
}
class BlueMarble : Marble { ... }

因此,我们现在了解细节,即的执行情况。 考虑以下呼吁:

Bag b = new RedBag();
b.GetMarbles();
b.DumpBag();

我谨印刷:

Bag of Marbles (aka "Marble", "RollyThing", Ruby"):
- Marble 1
- Marble 2
...

我们认为,为了印刷这一标题,Bag必须能够了解衍生类型的Marble,而不论实际情况如何。 本条对<条码>的“名称进行了分类,但同时也对衍生的<条码>。

DumpBag needs to do a kind of static virtual call . I ve started implementing DumpBag with the following:

public void DumpBag() {
    PropertyInfo pi = this.MarbleType.GetProperty("Nicknames", BindingFlags.Static);
    IEnumerable<string> nicknames = pi.GetValue(null, null);  // No instance

    StringBuilder sb = new StringBuilder("Bag of Marbles (aka ");
    foreach (string nn in nicknames)
        sb.Append(""" + nn + "", ");
    Console.WriteLine(sb.ToString());

    ...
}

我的问题:

  1. Is this sane? Hopefully I have (or I can) explain my rationale for why I ve gone this route.
  2. I get a warning (of course) that RedMarble.Nicknames hides Marble.Nicknames. Does it seem valid to go ahead and mark it new?
最佳回答

你发现,你再次失踪的是explicit。 :

(List<string>)this.MarbleType.GetProperty(“Nicknames”).GetValue(null, 无);

在我测试时,这给我打了罚款。

正如在评论中所讨论的那样,你不会真正使用新的关键词,你会把基类固定方法重新命名为其他方法,这样就没有模糊。 之后,你们都控制着这一点,而不是使用他人的法典。


Now, should you do it this way?

Well, first surely you want to use generics not defined methods to return types:

abstract class Bag<T> where T:marble {
    public void DumpBag()
    { 
        // here you can use
        // (IEnumerable<string>)typeof(T).GetProperty("Nicknames").GetValue(null, null);
    }
}

class RedBag : Bag<RedMarble> {
}

class BlueBag : Bag<BlueMarble> {
}

当然,你可以做的第二个事情是而不是静态<>>,在这种情况下,该财产将在<条码>中选取,在<条码>上取抽象,在和<条码>上封顶,然后在<条码>上取用。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签