Forcing casting one type of assembly to another type of assembly in C #
Is there a way to force different types of different assemblies? I need to execute a function whose assembly was loaded by Assembly.Load (ReadAllBytes (...)), but it didn't work when the arguments were taken. So, is there a way for "reinterpret_cast" objects in C #?
EDIT
The simplest example of my casting problem:
Assembly ass = Assembly.Load(File.ReadAllBytes("external.dll"))
object other_type_instance = ass.GetType("OtherType").InvokeMember(null, BindingFlags.CreateInstance, null, null, new Object[]{});
OtherType casted_isntance = (OtherType)other_type_instance; // fails with runtime error, because there are two OtherType:s classes loaded.
a source to share
If the two types do not have an inheritance relationship, the common interface or one of the types provides a conversion operator to the other ... no.
You will need to provide more details if you want more specific advice, but here's a general outline of casting behavior in C #.
Listing in C # can be either a save operation or a change to a view. When you pass an instance to a wider or narrower type in its inherited hierarchy or an interface that it implements, you perform a representation-preserving conversion. You are still dealing with the same bits, the compiler / runtime just ensures that the type you specify is valid for the instance of the object you are dealing with. Throwing through inherited hierarchies or interfaces not implemented by an instance is illegal.
For custom types, rendition castings are those that essentially create a new instance from an existing one. You can define your own cast operators for the type:
public class MyType
{
public static int implicit operator int( MyType t )
{
return 42; // trivial conversion example
}
}
Conversion operators can be defined as implicit
or explicit
-, which determines whether the compiler will apply them for you (implicit) or ask you to explicitly specify the type when you want the conversion (explicit).
Given what you are describing, you probably need to write a utility class that converts from one type to another. Unfortunately, there is nothing in C # or .NET that can do this for you.
a source to share
If you have two different versions of a loaded assembly and both classes are compatible (same assembly version or no change for a specific class), one is to serialize to memory from one instance and deserialize to an instance into the assembly you need. If there may be differences between the classes as a result of changes in the new assembly, you have more general serialization issues to consider. If so, I recommend looking at the DataContractSerializer , which can support more complex scenarios.
a source to share
Finally managed to cast between incompatible types using a combination of DynamicMethod, Emit-namespace and MethodUtil.ReplaceMethod (from http://www.codeproject.com/KB/dotnet/CLRMethodInjection.aspx ). It's hard to say which ones are needed and which are optional if you're only interested in casting ... I'm just glad the external invokation function works, even though these are arguments from different assemblies ...
a source to share
C # allows undefined typing such as var and object, although the two are completely different in behavior. For example, suppose you have serialized a class or structure and cannot use it for use in the Array.Copy method. One way is to go back to your instance of a class or structure and use it as an "object" instead. This way the compiler will allow you to use it almost anywhere you need it.
Of course, you will lose type safety for this class, but for small pieces of code it will be a more manageable solution than creating the whole cast method.
a source to share