I'm creating a type at runtime using Reflection.Emit
. The problem is that whenever I instantiate an instance of the new type I have to use object
or dynamic
because the type isn't known at compile time. This works fine except for when I would want another type to implicitly cast to the new type during assignment. The variable happily takes on the new value and it's corresponding type without attempting to cast to its current type.
Is there any way to create a variable of a newly created type that will allow for implicit casting? I'm perfectly happy to give up compile-time checking but I would like these casts to at least be attempted at run-time.
Edit:
Here is an example to make it more clear. This is what happens when you know the type at compile-time:
MyClass a;
//this calls the implicit cast operator and 'a' stays of the same type
a = 5;
and this is what happens if you don't:
Type t = CreateTypeUsingTypeBuilder();
object a = Activator.CreateInstance(t);
//this does not call the implicit cast operator and 'a' just becomes in integer
a = 5;
Also, I'm not surprised at this behavior or asking why it happens. I'm asking if there is any sort of workaround to achieve the desired behavior of having it check for an implicit operator at run-time.
If the idea is, as I suppose in this context, to create wrapper classes for known types in runtime, then I have a few ideas you could try.
One is to add a parameterized constructor in the wrapper type, that takes an object of the wrapped type as the parameter and then uses that object to initialize the wrapper object (like you would do in an implicit conversion operator). Then, knowing that you can use e.g. an integer as a constructor parameter, you could simply do this:
This will search the type for a constructor that could be invoked with the given object. You might get
MissingMethodException
if no matching public constructor is found.Another idea is to create either an interface or an abstract base type for your types built in runtime, that requires the derived classes to implement some sort of conversion method. When you derive your built types from this abstract class or make them implement the interface, you can cast the object created with the
Activator
to that class or that interface and then call the conversion method already normally. If you make your interface generic, you could have multiple different conversion implementations.While there is no way to use an implicit operator implicitly, I think you could achieve your ultimate goal of converting an integer to your built type in some other way, including one or the other hinted above.