c# - How to create correct InstanceDescriptor for initializing some properties -
lets have class this:
public class foo { public foo {} public int titi { get; set; } public int toto { get; set; } public int tata { get; set; } }
i can initialize new instance this:
var inst = new foo { titi = 12, toto = 42, tata = 421 };
but how can create correct instance descriptor performs same initialization above?
public class fooconverter : typeconverter { // ... public override object convertto(itypedescriptorcontext context, cultureinfo culture, object value, type destinationtype) { if (destinationtype == typeof(instancedescriptor) && value foo) { // incorrect example because properties wont // initialized 12, 42 , 421 var ctor = typeof(foo).getconstructor(type.emptytypes); return new instancedescriptor(ctor, null); } // ... } }
nb1: i'm asking because want create typeconverter 'foo' class , need provide convertion 'instancedescriptor'
nb2: yes, can add constructor foo class takes 3 arguments, avoid , know "construction description" corresponds above sample.
i don't know trying use seem instancedescriptor don't take constructors :
public static class fooserialization { public static foo createdesignfooinstance() { return new foo { titi = 12, toto = 42, tata = 421 }; } }
and :
var m = typeof(fooserialization).getmethod("createdesignfooinstance", bindingflags.static | bindingflags.public); var desc = new instancedescriptor(m, null);
Comments
Post a Comment