I use a dynamic keyword to work with an external assembly, and it works great with access to its methods and elements of a primitive type. So, for example, a class of a dynamically loaded class looks like this:
public class Student { public bool IsGood { get; set; } public StudentType St { get; set; } public University University { get; set; } }
I can dynamically load an object from an assembly by doing something like:
var assembly = Assembly.LoadFrom("//path"); Type type = assembly.GetType("TestFrameWork.Student"); var student = Activator.CreateInstance(type);
The following code could not be executed:
student.IsGood = true,
student.St = TestFrameWork.StudentType.SomethingElse;
Student type is an enumeration from a dynamically loaded assembly;
I can get the student object. Now for the interesting part. I can call it methods. I can get all its properties. I can set its primitive properties
So I can pretty much do student.IsGood = true; and he will set this property. The same is true if I had other primitive properties like int, float, etc.
BUT
When I try to set it to a property that is native to a dynamically loaded assembly, it does not work with a RuntimeBinderException
So for example
if I try to make student.University = new University (), where University is the native type of the loaded assembly. It fails.
Here is the stack trace:
in System.RuntimeTypeHandle.CreateInstance (RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean & canBeCached, RuntimeMethodHandleInternal & ctor, Boolean & bNeedSecurityCheck) in System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & stackMark) when System .RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & stackMark)
in System.Activator.CreateInstance (Type of type, Boolean nonPublic) in System.RuntimeType.CreateInstanceImpl (BindingFlags bindingAttr, Binder binder, Object [] args, CultureInfo culture, Object [] activAttributes, StackCrawlMark & stackMark) at System.Activator (stack. Type type, BindingFlags bindingAttr, Binder binder, Object [] args, CultureInfo culture, Object [] activAttributes) in System.Reflection.Assembly.CreateInstance (String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binding binder, Object [] args, CultureInfo , Object [] activationAttributes) in System.Reflection.Assembly.CreateInstance (String typeName) in TaskManagementFramework.PluginModule.CreateInstanceT in C: \ Dropbox \ CPTFramework_DynamicLoading \ TaskManagementFramework \ Plugin Loading \ PluginModuleManuleLanguinGumentulePlugin.luginMluguleLanguinGuinMan String id, String parentXmlSe ctionDescription, Type expectedInterface, Boolean useSingleInstance, IPlugin & plugin) in C: \ Dropbox \ CPTFramework_DynamicLoading \ TaskManagementFramework \ Plugin Download \ PluginLifecycleManager.cs: line 53 in TaskManagementFrameworkLmentMlementleMlementleMlementleMlementleMlementleMlementLmentMLElLEMLEMLEMlElMlElMlElleMlleLmentLaLleMleLentLentLentLentemLentLentemLentLaLleMleLentLentLentMentemLentLentmeLentmeLentemLentLaMleLemLentLentLentMentIFLentLentermain & plugins) in C: \ Dropbox \ CPTFramework_DynamicLoading \ TaskManagementFramework \ XML Parsing \ PluginsXmlParser.cs: line 39
Any idea why? I searched through the Internet for nothing specifically addressing this isssue ..