简体   繁体   English

如何在通用 class 的具体 class 中引用类型

[英]how to reference the Type in the Concrete class of a generic class

I found the following example, to which I have a follow up question.我找到了以下示例,对此我有一个后续问题。

stack overflow question 堆栈溢出问题

the existing code from the question is问题中的现有代码是

 public interface IRepository<T> where T : EntityObject
 {
    RepositoryInstructionResult Add(T item);
    RepositoryInstructionResult Update(T item);
    RepositoryInstructionResult Delete(T item);
 }

 public class Repository<T> : IRepository<T> where T : EntityObject
 {
    virtual RepositoryInstructionResult Add(T item)
    { //implementation}
    virtual RepositoryInstructionResult Update(T item);
    { //implementation}
    virtual RepositoryInstructionResult Delete(T item);
    { //implementation}
  }

public class BarRepository : Repositorybase<Bar>
 {
    public override RepositoryInstructionResult Update(Bar item);
    {
       //Call base method if needed
       //Base.Update(item);

       //implement your custom logic here
    }
  }

what I would like to do is change the Update method to something like我想做的是将 Update 方法更改为类似

public class BarRepository : Repositorybase<Bar>
 {
    // T is of Type Bar
    public override RepositoryInstructionResult Update(T item);
    {
       //implement your custom logic here
    }
  }

Question: is there a way to expose the generic type in BarResposity: Repositorybase<Bar> to the methods in BarRepository?问题:有没有办法将BarResposity: Repositorybase<Bar>中的泛型类型暴露给 BarRepository 中的方法?

looking for a better alternative to "search and replace" when building out the concrete class (eg make a copy of BarRespository as FooRepository and change all references from Bar to Foo).在构建具体的 class 时寻找“搜索和替换”的更好替代方法(例如,将 BarRespository 复制为 FooRepository 并将所有引用从 Bar 更改为 Foo)。 I would rather change the type in one place only.我宁愿只在一个地方改变类型。

(edit) Usage needs to remain as (编辑)使用需要保持为

var obj = new BarRepository();

Just as a note, if you're going to override all Add/Update/Delete anyways, you can make them as abstract in the RepositoryBase and then the vs suggestion is your friend:就像一个注释,如果你要覆盖所有的 Add/Update/Delete ,你可以在 RepositoryBase 中将它们作为抽象,然后 vs 建议是你的朋友: 在此处输入图像描述

and if there is shared logic between all of concrete classes you can put it in the abstract class and override abstract protected methods instead.如果所有具体类之间存在共享逻辑,则可以将其放在抽象 class 中,并改写抽象受保护方法。

But what is wrong with this但这有什么问题

public class BarRepository<T> : Repository<Bar> where T : class
{
    public override void Update(Bar item)
    {

    }
    
    public  void Update(T item)
    {
     
    }
}

It's a bit of a hack, but you could use a using alias to define the entity type:这有点 hack,但您可以使用using别名来定义实体类型:

 using MyType = Bar;

 public class BarRepository : Repositorybase<MyType>
 {
    public override RepositoryInstructionResult Update(MyType item);
    {
   
       return base.Update(item);
    }
  }

Now when you copy to Foo.cs , you can just change the using directive to现在,当您复制到Foo.cs时,您只需将 using 指令更改为

 using MyType = Foo;

But, I would look to try and reuse as much generic code as possible, as it's not clear at all what MyType is just by looking at the methods.但是,我会尝试尽可能多地重用通用代码,因为仅通过查看方法就不清楚MyType是什么。 There's nothing wrong with a find.replace to define a new repository type that customizes actions - you just want to keep the repeated to a minimum. find.replace 定义自定义操作的新存储库类型没有任何问题 - 您只想将重复保持在最低限度。

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

相关问题 如何从具有泛型返回类型的方法返回具体类的实例 - How to return instance of concrete class from method with generic return type 使用适当的具体类型创建泛型类的实例 - Create instance of generic class with proper concrete type 使用反射加载泛型类的具体类型 - Loading a concrete type of a generic class using reflection 基本类中具体类的类型,C#中具有泛型 - Type of concrete class in base class with generic in c# 使用Concrete类作为继承的通用基类的类型参数 - Using Concrete class as type parameter for inherited generic base class 如何将具体类注册到通用接口? - How to register Concrete Class to Generic Interface? 使用具体类型覆盖通用方法(类型参数*隐藏类*) - Override Generic Method With Concrete Type (type parameter * hides class *) 从接口序列化通用类时,如何让DataContractJsonSerializer在类型提示中使用具体类型 - How do I get DataContractJsonSerializer to use concrete type in type hint when serializing generic class from interface 类中没有泛型引用的泛型方法 - Generic Type Method Without Generic Reference in Class 在 C# 中为具体类中的任何类型实现泛型接口 - Implementing a generic interface for any type in a concrete class in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM