Home > Software design >  What is the best way to call a method defined inside an abstract generic class from a non generic cl
What is the best way to call a method defined inside an abstract generic class from a non generic cl

Time:09-16

I don't want to make class B generic because there lots classes inheriting from class B. How to use the method "GetSomeData" of the generic class "classA" in a non generic class?

Here's my code:

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
}

what is the best way to invoke GetSomeData in class B?

CodePudding user response:

First of all, your code doesn't look clean itself. C# coding conventions are not met & not all the components that are used present.

But to anser your question, you need to specify the concrete type T before using generic methods/classes.

For example working code may look like this:

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
{

}

CodePudding user response:

As per OOP principals Abstract classes can not be instantiated. They could only be Inherited. Well there are couple of ways how you can access the instance variables and methods from the Abstract class as follows.

  1. Make a sub-class inheriting the Abstract class; Create an object of sub-class and access all the Instance Variables and Methods.
    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. Make that method Static if its not tightly coupled to instance of the class. and then you can use it with 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. You can inherit this Abstract class in the class you want to use it and directly access it by base.MethodName or directly 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.
    }

In case 1 and 3 you will have to pass generic argument in order to inherit the Abstract class as in your case Abstract class accepts generic arguments.

  • Related