I want to use PostSharp follow code:
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();
}
}
in PostModerator I use follow code:
[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");
}
}
I have cast exception when I return new ReturnState<object>..
and Postshop try cast to ReturnState<Person>
but can't.
Yes, that's because you're creating a ReturnState<object>
, which isn't a ReturnState<Person>
.
Options:
PostModerateAttribute
to find out the correct return type, and create an instance of that instead.PostModerateAttribute
to return ReturnState<Person>
ReturnState<object>