I created a class that inherits DynamicObject
and want to create a static method that can create new instances with pre-determined properties (stored in the Dictionary
).
public class CustomDynamic : DynamicObject
{
protected Dictionary<string, object> InnerDictionary;
public static T Create<T>(Dictionary<string, object> dictionary) where T : CustomDynamic , new()
{
return new T
{
InnerDictionary = dictionary
};
}
}
Usage:
dynamic d = new Dictionary<string, object>();
var realPlayer = CustomDynamic.Create<Player>(d as Dictionary<string, object>);
var dynaPlayer = CustomDynamic.Create<Player>(d);
realPlayer // Player type according to VS2013
dynaPlayer // dynamic type according to VS2013
Since there is only one method signature, why does passing in a dynamic return a dynamic object? Or is actually just Visual Studio 2013 getting confused?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…