使用 C# 反射时无法创建类型 System.Fabric.FabricServiceNotFoundException 的通用 Class
Cannot make a Generic Class of Type System.Fabric.FabricServiceNotFoundException when using c# reflection
我有以下函数接收错误并创建 ErrorType<T>
的 class 实例
private dynamic GenerateDynamicErrorHandler(Exception ex)
{
try
{
string typeName = ex.GetType().FullName;
Type typeArgument = Type.GetType(typeName);
Type genericClass = typeof(ErrorType<>);
Type constructedClass = genericClass.MakeGenericType(typeArgument);
dynamic created = Activator.CreateInstance(constructedClass);
return created;
}
catch
{
throw new ReflectionException(ex.GetType().FullName);
}
}
然后 returns 一个 dynamic
对象在下面的代码片段中:
try
{
dynamic c = GenerateDynamicErrorHandler(ex);
if (c.IsError())
{
return c.GenericResponse<ErrorDetails,AppendingErrors>(isDebug,
isDefaultConverter,
context,
_errorDetailResponseFactory.CreateDefaultError(ex.Message,ex.InnerException,context.Response.StatusCode),
new AppendingErrors
{
Title = c.Get().Title,
ResultCode = c.Get().ResultCode,
StackTrace = ex.StackTrace
});
}
return HandleError.WithDefaultJsonConverter<ErrorDetails>(context,
_errorDetailResponseFactory.CreateDefaultError(ex.Message,ex.InnerException,context.Response.StatusCode));
}
catch(ReflectionException re)
{
return HandleError.WithGenericApendingJsonConverter<ErrorDetails, object>(context,
_errorDetailResponseFactory.CreateDefaultError(ex.Message, ex.InnerException, context.Response.StatusCode),
new {
reflectionError = re.Message
});
}
这适用于除 System.Fabric.FabricServiceNotFoundException
之外的所有异常,我不明白或不知道为什么。如果有人可以提供帮助,将不胜感激
感谢@Flydog57,问题实际上是获取类型的名称,然后尝试从该名称创建类型。正如评论中所说,简单的解决方法是使用
中传递的异常类型
private dynamic GenerateDynamicErrorHandler(Exception ex)
{
try
{
Type typeArgument = ex.GetType();
Type genericClass = typeof(ErrorType<>);
Type constructedClass = genericClass.MakeGenericType(typeArgument);
dynamic created = Activator.CreateInstance(constructedClass);
return created;
}
catch(Exception e)
{
throw new ReflectionException(ex.GetType().FullName);
}
}
我有以下函数接收错误并创建 ErrorType<T>
private dynamic GenerateDynamicErrorHandler(Exception ex)
{
try
{
string typeName = ex.GetType().FullName;
Type typeArgument = Type.GetType(typeName);
Type genericClass = typeof(ErrorType<>);
Type constructedClass = genericClass.MakeGenericType(typeArgument);
dynamic created = Activator.CreateInstance(constructedClass);
return created;
}
catch
{
throw new ReflectionException(ex.GetType().FullName);
}
}
然后 returns 一个 dynamic
对象在下面的代码片段中:
try
{
dynamic c = GenerateDynamicErrorHandler(ex);
if (c.IsError())
{
return c.GenericResponse<ErrorDetails,AppendingErrors>(isDebug,
isDefaultConverter,
context,
_errorDetailResponseFactory.CreateDefaultError(ex.Message,ex.InnerException,context.Response.StatusCode),
new AppendingErrors
{
Title = c.Get().Title,
ResultCode = c.Get().ResultCode,
StackTrace = ex.StackTrace
});
}
return HandleError.WithDefaultJsonConverter<ErrorDetails>(context,
_errorDetailResponseFactory.CreateDefaultError(ex.Message,ex.InnerException,context.Response.StatusCode));
}
catch(ReflectionException re)
{
return HandleError.WithGenericApendingJsonConverter<ErrorDetails, object>(context,
_errorDetailResponseFactory.CreateDefaultError(ex.Message, ex.InnerException, context.Response.StatusCode),
new {
reflectionError = re.Message
});
}
这适用于除 System.Fabric.FabricServiceNotFoundException
之外的所有异常,我不明白或不知道为什么。如果有人可以提供帮助,将不胜感激
感谢@Flydog57,问题实际上是获取类型的名称,然后尝试从该名称创建类型。正如评论中所说,简单的解决方法是使用
中传递的异常类型private dynamic GenerateDynamicErrorHandler(Exception ex)
{
try
{
Type typeArgument = ex.GetType();
Type genericClass = typeof(ErrorType<>);
Type constructedClass = genericClass.MakeGenericType(typeArgument);
dynamic created = Activator.CreateInstance(constructedClass);
return created;
}
catch(Exception e)
{
throw new ReflectionException(ex.GetType().FullName);
}
}