【发布时间】:2019-09-18 08:14:35
【问题描述】:
我希望能够将类名作为参数传递给方法,然后在该方法中创建具有某些参数的该类的对象。
一个具体(简化)的例子:
这是一种计算 OperationResult 的方法
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.");
}
根据我们从中获取错误的操作,我们使用不同的 ErrorKeyResolver。我想将这些 ErrorKeyResolver 作为参数传递给方法,这样我就不需要为每种错误类型制作不同的 GetFailedOperationResult 方法了。
灵感来自How to use class name as parameter in C# 我试过这样的事情:
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.");
}
但我不能这样做typeof(resourceKeyResolver),因为我不能使用变量作为类型。
有什么好的方法可以做到这一点吗?这甚至是一件好事吗?我还读到应该避免动态,所以我想知道在这里保存一些代码重复是否值得。
EDIT:输入参数应为:private IOperationResult<Unit> GetFailedOperationResult(IEnumerable<StrictSide> sides,IFailedOperationInfo failedOperationResult, string resourceKeyResolver)
从类名作为字符串我应该能够找到类型。
【问题讨论】:
-
typeof(resourceKeyResolver)应该是resourceKeyResolver.GetType()。 -
你可以使用Inversion Of Control
-
它没有意义......如果你有它的实例(
resourceKeyResolver),为什么要创建由resourceKeyResolver.GetType()表示的类的实例? ...这看起来像 XY 问题 -
@Selvin 你是对的。实际上我理解输入参数应该只是作为字符串的类名,而不是它的实例。我会更新问题。
-
Activator.CreateInstance的重载方法需要 2 个字符串 - 程序集和类名...问题出在哪里?
标签: c# parameter-passing