简体   繁体   English

C#继承Open Generics Compile

[英]C# Inherited Open Generics Compile

Today my brain went dead, and I couldn't figure out a clean way of forcing the compiler to use inheritance for Generic inference. 今天我的大脑已经死了,我无法想出一种强制编译器使用继承进行通用推理的简洁方法。

Imagine the following 4 classes 想象一下以下4个班级

Models 楷模

public abstract class Model
{

}

public class CodePerfModel : Model
{

}

Entities 实体

public abstract class ModelEntity<TModel> where TModel : Model
{
    public TModel Model { get; set; }

}

public class CodePerfEntity : ModelEntity<CodePerfModel>
{

}

Now to me logically I should take for granted that when I take something that inherits from ModelEntity<> (it will specify the type of TModel ) via inheritance, because any class that inherits from ModelEntity<> will have to specify it. 现在对我来说逻辑上我应该理所当然地认为当我从继承ModelEntity<> (它将指定TModel的类型)的东西时,因为任何继承自ModelEntity<>都必须指定它。

Is there anyway to force the compiler to figure this out for me? 反正有没有迫使编译器为我解决这个问题?

Eg 例如

If I currently want to use ModelEntity<> , I have to specify a type for it. 如果我目前想要使用ModelEntity<> ,我必须为它指定一个类型。 Such as the following: 如下:

public class CallerClass<TEntity, TModel>
    where TEntity : ModelEntity<TModel>
    where TModel : Model
{

}

How can I get rid of the TModel argument everywhere? 我怎样才能摆脱TModel争论呢? While still having access to the TModel type at compile time? 虽然在编译时仍然可以访问TModel类型? Eg via the base Model property. 例如,通过基本的Model属性。

To me, something like the following: 对我来说,类似于以下内容:

public class CallerClass<TEntity>
    where TEntity : ModelEntity<>
{

}

Would make perfect sense as when calling it all I should have to speicfy is eg 完全有道理,因为在调用它时我应该特别注意例如

SomeCall<CodePerfEntity>();

rather than 而不是

SomeCall<CodePerfEntity, CodePerfModel>();

Is this something that is currently possible? 这是目前可能的吗?

Would this be worth raising for C# 6/7? C#6/7值得提升吗?

You mention you would like to access TModel at compilation time, while not explicitly specifying this type when deriving a class. 您提到您希望在编译时访问TModel ,而在派生类时未明确指定此类型。 Letting go of your example, and moving to a more general case, this means you would like the semantics to remain the same, however you would not like to explicitly declare the type parameter's own type parameters when declaring a generic constraint. 放弃你的例子,转向更一般的情况,这意味着你希望语义保持不变,但是你不想在声明泛型约束时显式声明类型参数自己的类型参数。

In essence, you are asking why a specific syntax sugar feature is not implemented. 实质上,您在问为什么没有实现特定的语法糖功能。

Let's consider another example: 让我们考虑另一个例子:

public class CallerX<A, B> where A : ModelEntity<> where B : ModelEntity<>

From the example in your question, the compiler should insert TModel'1 and TModel'2 as type parameters for A and B respectively. 根据您的问题中的示例,编译器应分别插入TModel'1TModel'2作为AB类型参数。 Let's say the feature is implemented. 假设该功能已实施。 This means that we have created the default situation that TModel'1 and TModel'2 are different types, each having constraints that match the single type. 这意味着我们已经创建了TModel'1TModel'2是不同类型的默认情况, TModel'1类型都具有与单一类型匹配的约束。 What if I would like to add more constraints to either TModel'1 or TModel'2 , or force them to be the same type? 如果我想为TModel'1TModel'2添加更多约束,或强制它们是同一类型,该怎么办? Why is this case so special that it deserves its own syntax? 为什么这个案例如此特殊以至于它应该有自己的语法?

From what I know of the C# team, they have the policy that each new feature starts with "-100 points" and should be really great in order to be considered (see UserVoice for C#). 根据我对C#团队的了解,他们的策略是每个新功能都以“-100点”开头,并且应该非常好才能被考虑(参见C#的UserVoice )。

To summarize: 总结一下:

  • New language features are expensive and add complexity. 新语言功能昂贵且增加了复杂性。
  • You are asking for an implicit syntax for which it is unlikely/unclear that it will be the desired situation in most of the cases. 您要求的是一种隐式语法,在大多数情况下,它不太可能/不清楚它是否是所需的情况。
  • Developers will have to learn and understand that an open generic type as a type parameter constraint will insert a hidden and anonymous extra parameter. 开发人员必须学习并理解作为类型参数约束的开放泛型类型将插入隐藏和匿名的额外参数。 To me, it is not intuitive that some other type parameter has been added to my type without me having declared it. 对我来说,如果没有声明它,我的类型中添加了一些其他类型参数并不直观。

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

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