简体   繁体   English

尝试通过反射获取文字

[英]Trying to get a literal via reflection

Suppose I have a the following code: 假设我有以下代码:


Container.cs Container.cs

public class Container
{
    public readonly string IdType;

    public Container( string aIdType )
    {
        IdType = aIdType;
    }
}

SuperContainerA .cs SuperContainerA .cs

public class SuperContainerA : Container
{
    public SuperContainerA( /*x parameters*/ ) : base("A") {}
}

SuperContainerB.cs 超级容器B.cs

public class SuperContainerB : Container
{
    public SuperContainerB(  /*y parameters*/ ) : base("B") {}
}

SuperContainerToTheInfinityAndBeyond.cs SuperContainerToTheInfinityAndBeyond.cs

public class SuperContainerB : Container
{
    public SuperContainerB(  /*y parameters*/ ) : base("It's Over 9000!") {}
}

Based on that, what I'm trying to retrieve is the "A" and "B" that are being sent to the TypeId from the constructor. 基于此,我尝试检索的是从构造函数发送到TypeId“ A”“ B”

The catch here is... I need to retrieve those values during the initialization of the program, before creating an instance of those classes, so I thought that using reflection is my best bet here. 这里的问题是...我需要在程序初始化期间检索这些值,然后再创建这些类的实例,因此我认为使用反射是我最好的选择。 (Note: Creating an instance of the classes to retrieve the value would be valid if the number of parameters for each constructor would be the same, but they can change. :() (注意:如果每个构造函数的参数数量相同,但是创建类的实例以检索值将是有效的,但是它们可以更改。:()

Is it possible to use reflection to check the literals of my source code and/or Assemblies? 是否可以使用反射来检查我的源代码和/或程序集的文字? (If I can see something like the source code, then I can use Regex to get the value.)(Note: Including the sources as resource files to my program is not an option :P) (如果可以看到类似源代码的内容,则可以使用Regex来获取值。)(注:不能将源作为资源文件包含到程序中:P)

I'm thinking of declaring constants to hold the value and force an naming rule on then, so that I could use reflection later to grab then back. 我正在考虑声明常量以保留该值并在那时强制使用命名规则 ,以便稍后可以使用反射来获取然后返回。 Something like ... 就像是 ...

public class SuperContainerA
{
    public const string ctIdType = "A";
}

public class SuperContainerB
{
    public const string ctIdType = "B";
}

... But I'm not sure if this is the best approach to this problem, since I won't have anything to help me check if these consts have been declared and if they got the proper name during compile time. ...但是我不确定这是否是解决此问题的最佳方法,因为我没有任何帮助来检查这些const是否已声明以及它们在编译时是否具有正确的名称。

Actually, if the language had some kind of static inheritance, this would help a lot in this situation, but I hear some programmers complaing that static inheritance is more of an head ache than a cure. 实际上,如果该语言具有某种静态继承,那么在这种情况下这将有很大帮助,但是我听说有些程序员抱怨静态继承更多是头痛而不是治愈。

Anyway, I'm searching for alternatives. 无论如何,我正在寻找替代品。 Any idea is welcome. 任何想法都欢迎。

Attributes to the rescue! 救援的属性!

public class IdTypeAttribute: Attribute
{
    public string IdType { get; private set; }

    public IdTypeAttribute(string idType)
    {
        IdType = idType;
    }
}

[IdType("B")]
public class SuperContainerB: Container
{
    // whatever you like...
}

You can then access the Attribute via reflection. 然后,您可以通过反射访问属性。 Easy enough to do... 足够容易做...

var type = typeof(SuperContainerB);
var attribute = (IdTypeAttribute)type.GetCustomAttributes(
    typeof(IdTypeAttribute), false)[0];
var idType = attribute.IdType;

Why not simply use the concrete type to look up the string value that you seem to want associated with it? 为什么不简单地使用具体类型来查找您想要与之关联的字符串值?

public class SuperA : Container 
{
    public string IdType { get { return IdTypeFactory.Get( GetType() ); } }
}

public static class IdTypeFactory
{
    public static string Get( Type containerType ) { ... }
}

The primary benefit of this solution would be to gather all your string literals in one central location. 此解决方案的主要好处是将所有字符串文字收集在一个中央位置。 Alternatively, go with the abstract super class. 或者,使用抽象超类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM