[TryCatch]
public int Do(int t = 5)
{
throw new("");
}
[TryCatch<int>]
public int Do2(int t = 5)
{
throw new("");
}
Unhandled exception. System.TypeLoadException: Could not load type 'AOPUtil.TryCatchAttribute`1' from assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
我通过ILSPY查看源码
【正常版本TryCatchAttribute 】
[TryCatch]
public int Do(int t = 5)
{
TryCatchAttribute tryCatchAttribute = new TryCatchAttribute();
IMo[] mos = new IMo[1] { tryCatchAttribute };
MethodContext methodContext = new MethodContext(this, typeof(Class2), MethodBase.GetMethodFromHandle((RuntimeMethodHandle)/*OpCode not supported: LdMemberToken*/, typeof(Class2).TypeHandle), isAsync: false, isIterator: false, mosNonEntryFIFO: false, mos, new object[1] { t });
try
{
throw new Exception("");
}
catch (Exception exception)
{
methodContext.Exception = exception;
tryCatchAttribute.OnException(methodContext);
if (methodContext.ExceptionHandled)
{
return (int)methodContext.ReturnValue;
}
throw;
}
}
【错误版本TryCatchAttribute<T>】
[TryCatch<int>]
public int Do2(int t = 5)
{
TryCatchAttribute<> tryCatchAttribute = (TryCatchAttribute<>)(object)new TryCatchAttribute<int>();
IMo[] mos = new IMo[1] { tryCatchAttribute };
MethodContext methodContext = new MethodContext(this, typeof(Class2), MethodBase.GetMethodFromHandle((RuntimeMethodHandle)/*OpCode not supported: LdMemberToken*/, typeof(Class2).TypeHandle), isAsync: false, isIterator: false, mosNonEntryFIFO: false, mos, new object[1] { t });
try
{
throw new Exception("");
}
catch (Exception exception)
{
methodContext.Exception = exception;
((TryCatchAttribute<T>)(object)tryCatchAttribute).OnException(methodContext);
if (methodContext.ExceptionHandled)
{
return (int)methodContext.ReturnValue;
}
throw;
}
}
我设置了这样两条特性,
TryCatchAttribute
可以正常使用 但是TryCatchAttribute<T>
出错。 代码上他们长这样我通过ILSPY查看源码 【正常版本
TryCatchAttribute
】【错误版本
TryCatchAttribute<T>
】IL