如何调用具有约束的通用参数的静态方法 <T, TRepository>() where TRepository:IRepository<T>,new()
How to call static method which has generic paremeters with constraints as <T, TRepository>() where TRepository:IRepository<T>,new()
我在 RepositoryFactory.cs
中定义了一个名为 (GetRepositoryInstance) 的静态方法
public class RepositoryFactory
{
public static TRepository GetRepositoryInstance<T, TRepository>() where TRepository:IRepository<T>,new()
{
return new TRepository();
}
}
所以当我试图在另一个名为 OrganizationCommonRepository.cs 的 class 中实现此方法时。它抛出错误
"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();
}
}
}
我有 Employee.cs , EmployeeRepository.cs 和接口 IEmployeeRepository ,IRepository.
但我仍然不确定这里应该有什么问题。 Image of Error
public interface IEmployeeRepository:IRepository<Employee>
{
}
如果有人能帮我解决这个错误就太好了。
谢谢
塞尔瓦阿南德
EmployeeRepository.cs
IRepository.cs
快速修复
调用泛型方法时,类型参数应在尖括号 <>
而不是 ()
中传递。所以 GetRepositoryInstance
在
OrganizationCommonRepositor
应该是这样的:
RepositoryFactory.GetRepositoryInstance<Employee, EmployeeRepository>()
错误解释
错误表明 Employee
和 EmployeeRepository
是类型,它们在它们使用的上下文中无效。而且这个错误是明确的,它正确地指出了问题(我也相信它指出了这些类型使用不正确的行号)。
让我们更仔细地看看原始方法调用。
RepositoryFactory.GetRepositoryInstance(Employee, EmployeeRepository)();
从 C# 的角度来看,这个表达式等价于这个:
var func = RepositoryFactory.GetRepositoryInstance(Employee, EmployeeRepository);
func();
因此,正如您所见,C# 编译器将第一对括号 ()
视为方法调用(这又 returns 由于另一对括号 ()
而成为可调用的东西).由于这是一个方法调用,C# 编译器认为 Employee
和 EmployeeRepository
是您传递给该方法的常规参数,并且该方法不是通用的。并且由于参数应该是有效对象(在运行时占用一些内存的东西),因此不允许在此处使用类型名称,因为它们不是运行时对象。
这就是编译器说它们是类型并在无效上下文中使用的原因。类型名称的有效上下文之一是泛型。
我在 RepositoryFactory.cs
中定义了一个名为 (GetRepositoryInstance) 的静态方法public class RepositoryFactory
{
public static TRepository GetRepositoryInstance<T, TRepository>() where TRepository:IRepository<T>,new()
{
return new TRepository();
}
}
所以当我试图在另一个名为 OrganizationCommonRepository.cs 的 class 中实现此方法时。它抛出错误
"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();
}
}
}
我有 Employee.cs , EmployeeRepository.cs 和接口 IEmployeeRepository ,IRepository.
但我仍然不确定这里应该有什么问题。 Image of Error
public interface IEmployeeRepository:IRepository<Employee>
{
}
如果有人能帮我解决这个错误就太好了。
谢谢
塞尔瓦阿南德
EmployeeRepository.cs
IRepository.cs
快速修复
调用泛型方法时,类型参数应在尖括号 <>
而不是 ()
中传递。所以 GetRepositoryInstance
在
OrganizationCommonRepositor
应该是这样的:
RepositoryFactory.GetRepositoryInstance<Employee, EmployeeRepository>()
错误解释
错误表明 Employee
和 EmployeeRepository
是类型,它们在它们使用的上下文中无效。而且这个错误是明确的,它正确地指出了问题(我也相信它指出了这些类型使用不正确的行号)。
让我们更仔细地看看原始方法调用。
RepositoryFactory.GetRepositoryInstance(Employee, EmployeeRepository)();
从 C# 的角度来看,这个表达式等价于这个:
var func = RepositoryFactory.GetRepositoryInstance(Employee, EmployeeRepository);
func();
因此,正如您所见,C# 编译器将第一对括号 ()
视为方法调用(这又 returns 由于另一对括号 ()
而成为可调用的东西).由于这是一个方法调用,C# 编译器认为 Employee
和 EmployeeRepository
是您传递给该方法的常规参数,并且该方法不是通用的。并且由于参数应该是有效对象(在运行时占用一些内存的东西),因此不允许在此处使用类型名称,因为它们不是运行时对象。
这就是编译器说它们是类型并在无效上下文中使用的原因。类型名称的有效上下文之一是泛型。