【问题标题】:What is the best way to call a method defined inside an abstract generic class from a non generic class从非泛型类调用抽象泛型类中定义的方法的最佳方法是什么
【发布时间】:2021-09-14 18:53:48
【问题描述】:

我不想让B类泛型,因为有很多类继承自B类。如何在非泛型类中使用泛型类“classA”的方法“GetSomeData”?

这是我的代码:

public abstract class classA<T> : IInterface<T>
    where T : new()
{
  public String GetSomeData (Guid ID)
  {
    return somestring;       
  }
}

public abstract class ClassB : InterfaceB
{
//Use GetSomeData(Guid ID) here
}

在 B 类中调用 GetSomeData 的最佳方法是什么?

【问题讨论】:

  • 非常不清楚这两种类型如何相互关联。您想要实现但无法编译的代码示例会很有帮助。

标签: c# .net generics


【解决方案1】:

首先,您的代码本身看起来并不干净。未满足 C# 编码约定,并且并非所有使用的组件都存在。

但要回答您的问题,您需要在使用泛型方法/类之前指定具体类型 T。

例如,工作代码可能如下所示:

public abstract class GenericParent<T> 
  where T : new()
{
    public string GetSomeData(Guid id) => string.Empty;
}

// Non abstract type, to create be able to create an instance
public class GenericChild<T> : GenericParent<T> where T : new()
{

}

public abstract class ClassB
{
    public void DoSomething()
    {
        // Creating instance of a generic type, specifying concrete T (in this case of type SomeClass)
        var instance = new GenericChild<SomeClass>();
        instance.GetSomeData(Guid.Empty);
    }
}

// An example of type, that meets the "where" condition of T in the generic type
public class SomeClass
{

}

【讨论】:

    【解决方案2】:

    根据 OOP 原则,抽象类不能被实例化。 他们只能被继承。有几种方法可以访问 Abstract 类中的实例变量和方法,如下所示。

    1. 创建一个继承抽象类的子类;创建一个子类对象并访问所有实例变量和方法。
        public abstract class classA<T> : IInterface<T>
                where T : ITask, new()
        {
        
          public String GetSomeData (Guid ID)
          {
            return somestring;       
          }
        
        }
        
        public class ChildOfClassA : ClassA<SomeType_of_Type_ITask>
        {
        }
    
        public abstract class ClassB : InterfaceB
        {
        //Use GetSomeData(Guid ID) here
        ChildOfClassA obj = new ChildOfClassA();
        string result = obj.GetSomeData(Guid.NewGuid());
        }
    
    1. 如果该方法与类的实例没有紧密耦合,则将该方法设为静态。然后您可以将其与ClassName.MethodName 一起使用
        public abstract class classA<T> : IInterface<T>
                where T : ITask, new()
        {
        
          public **static** String GetSomeData (Guid ID)
          {
            return somestring;       
          }
        
        }
        
        public abstract class ClassB : InterfaceB
        {
        //Use GetSomeData(Guid ID) here
        string result = classA.GetSomeData(Guid.NewGuid());
        }
    
    1. 你可以在你想使用的类中继承这个Abstract类,直接通过base.MethodName或者MethodName直接访问。
    public abstract class classA<T> : IInterface<T>
                where T : ITask, new()
        {
        
          public string GetSomeData (Guid ID)
          {
            return somestring;       
          }
        
        }
        
        public abstract class ClassB : ClassA<SomeType_of_Type_ITask>, InterfaceB
        {
        //Use GetSomeData(Guid ID) here
        string result = [base.]GetSomeData(Guid.NewGuid()); //[base.] is optional even you can override this function or overload it.
        }
    

    在情况 1 和 3 中,您必须传递泛型参数才能继承抽象类,因为在您的情况下,抽象类接受泛型参数。

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多