简体   繁体   English

在不知道类型参数的情况下,以字符串形式获取泛型的成员名称

[英]Get a generic's member names as strings without knowing the type parameters

I'm using reflection to get some member names as strings. 我正在使用反射来获取一些成员名称作为字符串。 I'm using a method I found on Google Code (but I don't have a strong grasp of the reflection/LINQ magic it uses): 我使用的是我在Google Code上发现的方法(但我对它使用的反射/ LINQ魔术没有很强的了解):

public static class MembersOf<T> {
    public static string GetName<R>(Expression<Func<T,R>> expr) {
        var node = expr.Body as MemberExpression;
        if (object.ReferenceEquals(null, node)) 
            throw new InvalidOperationException("Expression must be of member access");
        return node.Member.Name;
    }
}

This works great in static constructors. 这在静态构造函数中效果很好。 I just use it like this: 我只是这样使用它:

using ClassMembers = MembersOf<MyClass>;

class MyClass
{
    public int MyProperty { get; set; }

    static MyClass
    {
        string lMyPropertyName = ClassMembers.GetName(x => x.MyProperty);
    }
}

With this approach you avoid misspelling member names in string literals and can use automatic refactoring tools. 使用这种方法,可以避免在字符串文字中拼写错误的成员名称,并且可以使用自动重构工具。 It's nice to have when implementing INotifyPropertyChanged ! 实现INotifyPropertyChanged时很高兴!

But now I have a generic class that I want to use in the same manner, and I've learned that you can't use unbound types as generic type parameters: 但是现在我有了一个通用类,希望以相同的方式使用它,并且我了解到不能将未绑定类型用作通用类型参数:

using ClassMembers = MembersOf<MyGeneric<>>;

class MyGeneric<T> { }

What would be a good way to work around this problem? 解决此问题的好方法是什么?

最好的方法是忘记using指令,而直接使用该类:

string propName = MembersOf<MyGeneric<T>>.GetName(x => x.SomeProp);

D'oh! 天哪! The using ClassMembers alias was hiding the obvious. using ClassMembers别名隐藏了显而易见的东西。 I just need to use MembersOf<MyGeneric<T>> directly in my class! 我只需要在我的课程中直接使用MembersOf<MyGeneric<T>>

class MyGeneric<T>
{
    public int MyProperty { get; set; }

    static MyClass<T>
    {
        string lMyPropertyName = MembersOf<MyGeneric<T>>.GetName(x => x.MyProperty);
    }
}

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

相关问题 在不知道通用类型的情况下获取通用类型对象的属性 - Get Property of generic type object without knowing the generic type 不知道类型就返回通用 - Returning generic without knowing type 如何在不知道其特定类型的情况下获取通用接口? - How to get a generic interface without knowing its specific type? 创建没有在类上指定类型参数的通用类型成员变量 - Creating a generic typed member variable without type parameters specified on the class 在不知道类型的情况下引用泛型类型的实例 - Referencing instances of Generic Type without knowing the Type 如何在不知道类型的情况下转换泛型类型 - How to cast a generic type without knowing the type 在不知道类型的情况下使用泛型类型转换 object - Cast an object with a generic type without knowing the type 在不知道类型的情况下返回通用对象? - Returning a generic object without knowing the type? 如何在不知道泛型的情况下获取泛型类(单例)中静态字段的值 - How to get the value of a static field in a generic class (singleton) without knowing the generic type 如何在运行时从通用EntitySet中获取实体而又不知道通用类型? - How to get the entities from generic EntitySet on run-time without knowing the generic type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM