dimanche 7 février 2016

Can I set the type of a field within a class at runtime to a newly created type in c#?

I'm using Reflection.Emit and TypeBuilder to create a new type at runtime. My setup is something like:

public class MyClass {
    public object MyField = CreateInstanceOfNewType();
    public MyClass() {}
}

The issue is that MyClass.MyField is declared with type object and so the implicit cast operators that exist for the new type are not called when castable types are assigned to MyClass.MyField. Is there a way to set the type of the field to the newly created type so that it behaves similarly to how it would in the typical case?





Aucun commentaire:

Enregistrer un commentaire