I met a weird error in my job, it happened when I tried to access a dynamic property in a special method of a special class. After researching, I found the code that made that problem but I still don't know why.
Here is my code ( I use .NET 4.5 )
public class MyCommand<TResult>
: Command<MyCommand<TResult>>
where TResult : class
{
}
public class MyDacAction : DacActionBase<MyDacAction, MyCommand<string>>
{
public override void Execute()
{
dynamic x = new System.Dynamic.ExpandoObject();
x.AAA = 100;
int b = x.AAA;
}
}
public abstract class DacActionBase<TCommand, TDacCommand>
where TCommand : class
where TDacCommand : class, new()
{
public virtual void Execute()
{
}
}
public abstract class Command<TCommand>
: CommandBase<TCommand>
where TCommand : class
{
public virtual void Execute()
{
}
}
public abstract class CommandBase<TCommand> where TCommand : class
{
}
class Program
{
static void Main(string[] args)
{
var test = new MyDacAction();
test.Execute();
}
}
If you create a console app and run this code, you will see the StackOverflowException at this line
int b = x.AAA;
When testing, I found two changes that the error will not be threw
1.
public class MyCommand
: Command<MyCommand>
{
}
public class MyDacAction : DacActionBase<MyDacAction, MyCommand>
{
public override void Execute()
{
dynamic x = new System.Dynamic.ExpandoObject();
x.AAA = 100;
int b = x.AAA;
}
}
2.
public abstract class Command<TCommand>
where TCommand : class
{
public virtual void Execute()
{
}
}
Can you tell me why this error happened ?