我想使用PostSharp跟随代码:
class Program
{
[PostModerator]
public static ReturnState<Person> GetPerson()
{
return new ReturnState<Person>() { Result = new Person { Id = 2, Name = "Gholzam" }, Succeed = true };
}
static void Main(string[] args)
{
var personAttempt= GetPerson();
Console.WriteLine("Test ok");
Console.ReadKey();
}
}
在PostModerator中我使用以下代码:
[Serializable]
public class PostModeratorAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
args.ReturnValue = new ReturnState<object> { Succeed=false };//Star
args.FlowBehavior = FlowBehavior.Return;
for (int counter = 0; counter < args.Arguments.Count; counter++)
{
Console.WriteLine("arg{0} is {1}",counter,args.Arguments[counter]);
}
Console.WriteLine("On Entry");
}
}
当我返回new ReturnState<object>..
并且Postshop尝试强制转换为ReturnState<Person>
时,我会抛出异常,但不能。
答案 0 :(得分:2)
是的,那是因为您创建的ReturnState<object>
不是ReturnState<Person>
。
选项:
PostModerateAttribute
以找出正确的返回类型,并改为创建该实例。PostModerateAttribute
返回ReturnState<Person>
ReturnState<object>