C# 如何将类的名称作为参数传递给方法,并在方法中实例化该类?
我希望能够将类名作为参数传递给一个方法,然后在该方法中创建一个具有特定参数的类的对象 具体(简化)示例: 这是一种计算运算结果的方法C# 如何将类的名称作为参数传递给方法,并在方法中实例化该类?,c#,parameter-passing,C#,Parameter Passing,我希望能够将类名作为参数传递给一个方法,然后在该方法中创建一个具有特定参数的类的对象 具体(简化)示例: 这是一种计算运算结果的方法 private IOperationResult<Unit> GetFailedOperationResult(IEnumerable<StrictSide> sides, IFailedOperationInfo failedOperationResult) { var exception = failedOperationRes
private IOperationResult<Unit> GetFailedOperationResult(IEnumerable<StrictSide> sides, IFailedOperationInfo failedOperationResult)
{
var exception = failedOperationResult.Exception.HasValue() ? failedOperationResult.Exception.Value() : null;
if (exception != null)
{
return new FailedResult<Unit>(
new InvalidBundleErrorKeyResolver(new FailedOperationInfo(new OperationInfo(failedOperationResult.OperationName, sides), exception)));
}
throw new InvalidOperationException("Failed operation result during bundle consistency check does not contain error or exception.");
}
private IOperationResult GetFailedOperationResult(IEnumerable side,IFailedOperationInfo failedOperationResult)
{
var exception=failedOperationResult.exception.HasValue()?failedOperationResult.exception.Value():null;
if(异常!=null)
{
返回新的FailedResult(
新的InvalidBundleErrorKeyResolver(新的FailedOperationInfo(新的OperationInfo(failedOperationResult.OperationName,sides),exception));
}
抛出新的InvalidOperationException(“捆绑一致性检查期间失败的操作结果不包含错误或异常”);
}
根据获得错误的操作,我们使用不同的ErrorKeyResolver。我想将这些ErrorKeyResolver作为参数传递给方法,这样就不需要为每种错误类型创建不同的GetFailedOperationResult方法
灵感来自
我试过这样的方法:
private IOperationResult<Unit> GetFailedOperationResult(IEnumerable<StrictSide> sides,IFailedOperationInfo failedOperationResult, IErrorResourceKeyResolver resourceKeyResolver)
{
var exception = failedOperationResult.Exception.HasValue() ? failedOperationResult.Exception.Value() : null;
if (exception != null)
{
return new FailedResult<Unit>(Activator.CreateInstance(typeof(resourceKeyResolver),new FailedOperationInfo(new OperationInfo(failedOperationResult.OperationName, sides), exception)));
}
throw new InvalidOperationException("Failed operation result during bundle consistency check does not contain error or exception.");
}
private IOperationResult GetFailedOperationResult(IEnumerable side,IFailedOperationInfo failedOperationResult,IErrorResourceKeyResolver resourceKeyResolver)
{
var exception=failedOperationResult.exception.HasValue()?failedOperationResult.exception.Value():null;
if(异常!=null)
{
返回新的FailedResult(Activator.CreateInstance(typeof(resourceKeyResolver),new FailedOperationInfo(new OperationInfo)(failedOperationResult.OperationName,sides),exception));
}
抛出新的InvalidOperationException(“捆绑一致性检查期间失败的操作结果不包含错误或异常”);
}
但是我不能执行typeof(resourceKeyResolver)
,因为我不能使用变量作为类型
有什么好办法吗?这是一件好事吗?我还读到应该避免动态,所以我想知道在这里保存一些代码重复是否值得
编辑:输入参数应为:私有IOperationResult GetFailedOperationResult(IEnumerable sides,IFailedOperationInfo failedOperationResult,string resourceKeyResolver)
从类名字符串中,我应该能够找到类型。如果将类作为接口传递,则可以使用以下代码实例化解析器
var resolver = Activator.CreateInstance(resourceKeyResolver.GetType(),
new FailedOperationInfo(new OperationInfo(failedOperationResult.OperationName,
sides), exception));
否则,如果使用类名(程序集限定名),例如,可以先将其转换为type,然后再次调用上面的行
var resolverType = Type.GetType(resourceKeyResolverClassName);
var resolver = Activator.CreateInstance(resolverType ,
new FailedOperationInfo(new OperationInfo(failedOperationResult.OperationName,
sides), exception));
有关
GetType()
方法的文档,请参见typeof(resourceKeyResolver)
应该是resourceKeyResolver.GetType()
。您可以使用它,但它不会产生任何意义。。。如果您有类的实例(resourceKeyResolver
),为什么要创建由resourceKeyResolver.GetType()表示的类的实例。。。这看起来像XYproblem@Selvin你是对的。实际上,我理解输入参数应该只是作为字符串的类名,而不是它的实例。我将更新问题。有一个重载的Activator.CreateInstance
方法,它包含两个字符串-程序集和类名。。。问题出在哪里?Type.GetString(resourceKeyResolver)
你的意思是typetype.AssemblyQualifiedName
?是的,更新了说明并添加了文档链接以使其更清晰,现在它更令人困惑了。没有定义方法GetString
,只有GetType
。但是,您不能在resourceKeyResolver
实际是什么的实例上调用后者。谢谢@tchrikch。正如Himbrombeer所提到的,对于答案的第二部分,如果类名以字符串形式给出(这也是您发布的链接),那么我想您的意思是Type.GetType(String)。