Home > Back-end >  How to call static method which has generic paremeters with constraints as <T, TRepository>()
How to call static method which has generic paremeters with constraints as <T, TRepository>()

Time:03-06

I have defined a static method called (GetRepositoryInstance)in RepositoryFactory.cs

public class RepositoryFactory
{
   public static TRepository GetRepositoryInstance<T, TRepository>() where   TRepository:IRepository<T>,new()
     
    {
          return new TRepository();

    }
   
}

so when i am trying to implement this method in another class called OrganizationCommonRepository.cs .It is throwing the error

"CS0119: 'Employee' is a type ,which is not valid in the given context" "CS0119: 'EmployeeRepository' is a type ,which is not valid in the given context" .

public class OrganizationCommonRepository:IOrganizationCommonRepository

{
  
IEmployeeRepository Repository = RepositoryFactory.GetRepositoryInstance(Employee, EmployeeRepository)();

        public void Delete(Client ObjDeleteData)
        {
            throw new NotImplementedException();
        }

        public void Insert(Client ObjNewData)
        {
            throw new NotImplementedException();
        }

        public void Update(Client ObjUpdatedData)
        {
            throw new NotImplementedException();
        }
    }
}

I have Employee.cs , EmployeeRepository.cs and interface IEmployeeRepository ,IRepository.

But still i am not sure what should be the wrong here. Image of Error

public interface IEmployeeRepository:IRepository<Employee>
{

}

it would be great if any one could help me out to resolve this error.

Thanks

SelvaAnand

EmployeeRepository.cs

IRepository.cs

CodePudding user response:

Quick fix


When calling a generic method, type arguments should be passed in angle brackets <> instead of (). So the call of GetRepositoryInstance in the OrganizationCommonRepositor should be like this:

RepositoryFactory.GetRepositoryInstance<Employee, EmployeeRepository>()

Explanation of the error


The error says that Employee and EmployeeRepository are types and they aren't valid in the context they used. And the error is not ambiguous, it indicates the problem correctly (I also believe it points out to the line numbers where these types used incorrectly).

Let's take a more closer look to the original method call.

RepositoryFactory.GetRepositoryInstance(Employee, EmployeeRepository)();

From C#'s standpoint, this expression is equivalent to this one:

var func = RepositoryFactory.GetRepositoryInstance(Employee, EmployeeRepository);
func();

So as you can see the C# compiler treats the first pair of brackets () as a method call (which in turns returns something callable because of another pair of brackets ()). And since this is a method call, C# compiler thinks that Employee and EmployeeRepository are regular arguments you pass to the method and that the method is not generic. And since arguments should be valid objects (something that takes up some memory in runtime), it's not allowed to use type names here, because they aren't runtime objects.

This is why the compiler says that they are types and used in invalid context. One of the valid contexts for type names is generics.

  • Related