简体   繁体   English

将 nameof 用于递归泛型类型的属性

[英]Using nameof for a recursive generic type's property

I have the following class我有以下课程

public abstract class Result<TResult, TData> where TResult : Result<TResult, TData>
{
    public virtual TData Data { get; private set; }
}

How can I use nameof on the Data property?如何在 Data 属性上使用 nameof?

It tried some options but got compilation errors它尝试了一些选项但出现编译错误

nameof(Result<object, object>.Data)

Error   CS0311  
The type 'object' cannot be used as type parameter 'TResult' in the generic type or method 
'Result<TResult, TData>'. There is no implicit reference conversion from 'object' to 
'Result<object, object>'

UPDATE To better specify:更新为了更好地指定:

I did not write the class in the first snippet, but I have to use it as it is.我没有在第一个片段中编写类,但我必须按原样使用它。 I need to use reflection to get a PropertyInfo instance for the Data property.我需要使用反射来获取 Data 属性的 PropertyInfo 实例。 I can do it by calling GetProperty("Data"), but I prefer to avoid strings in case the name property is renamed with the Visual Studio Refactoring (for example from Data to Content).我可以通过调用 GetProperty("Data") 来实现,但我更喜欢避免使用字符串,以防使用 Visual Studio 重构(例如从数据到内容)重命名 name 属性。 I cannot use nameof(Data) and I am in a different context that doesn't know about the Data property in that class.我不能使用 nameof(Data) 并且我处于不同的上下文中,该上下文不知道该类中的 Data 属性。

As such, I would call GetProperty(nameof({whatever}.Data)).因此,我会调用 GetProperty(nameof({whatever}.Data))。 Of course, if not possible I would use the string.当然,如果不可能,我会使用字符串。 But if there is a way, I would like to know it.但如果有办法,我想知道。

Unfortunately, the nameof operator does not work with unbound generic types .不幸的是, nameof运算符不适用于未绑定的泛型类型 Eg you'd like to be able to write nameof(Result<,>.Data) , just like you can today write something like typeof(Result<,>) .例如,您希望能够编写nameof(Result<,>.Data) ,就像您今天可以编写typeof(Result<,>) So you would need to specify type parameters when trying to get the nameof(Result<TResult, TData>.Data) value.因此,在尝试获取nameof(Result<TResult, TData>.Data)值时,您需要指定类型参数。

Which you've tried, but you provided object as the type parameter for both parameters, even though your generic type constrains TResult as being derived from Result<TResult, TData> .您已经尝试过,但是您提供了object作为两个参数的类型参数,即使您的泛型类型将TResult限制为从Result<TResult, TData>派生。 The type object doesn't meet the constraint, so that can't possibly work.类型object不符合约束,因此不可能工作。 Hence the compiler error.因此编译器错误。

Obviously, if you can provide any type that does meet the constraint, that would solve the compiler error and allow you to use the nameof operator.显然,如果您可以提供满足约束的任何类型,那将解决编译器错误并允许您使用nameof运算符。 There's not enough information in your question to know whether that's an option in your specific scenario.您的问题中没有足够的信息来了解在您的特定情况下这是否是一个选项。

I agree with this comment that you would probably be better off asking a different question, one which takes a step back and explains how you arrived at feeling you needed this syntax in the first place.我同意这个评论,你可能最好问一个不同的问题,一个退后一步并解释你最初是如何感觉你需要这种语法的。 It's not clear what the broader goal you're trying to accomplish is, where you don't have known type parameters to use for this expression.目前尚不清楚您要实现的更广泛的目标是什么,您没有用于此表达式的已知类型参数。 Typically, code outside of the generic type that wants to make use of the generic type, would actually know the type parameters it intends to use with the generic type.通常,想要使用泛型类型的泛型类型之外的代码实际上会知道它打算与泛型类型一起使用的类型参数。

Note that in the context of the generic type itself, you can refer to the property without knowing the exact type parameters, since the property identifier does not need qualification.请注意,在泛型类型本身的上下文中,您可以在不知道确切类型参数的情况下引用属性,因为属性标识符不需要限定。 Eg nameof(Data) would work, for any code that's actually in the generic class Result<TResult, TData> .例如, nameof(Data)可以用于任何实际在泛型类Result<TResult, TData> Whether that helps in your specific scenario is unclear from the information provided so far.从目前提供的信息来看,这是否有助于您的特定场景尚不清楚。

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

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