You call the static method. With dynamic you bypass the check, but you're actually trying to call Concatenate() on System.Type for CodeInjection.DynConcatenateString .
First of all, do this with the instance method:
public class DynConcatenateString { public string Concatenate(string s1, string s2){ return s1 + "" ! "" + s2; } }
Now let's see your code:
dynamic type = results.CompiledAssembly.GetType("CodeInjection.DynConcatenateString");
This is a System.Type , not an object of type CodeInjection.DynConcatenateString . If you change dynamic to var , at compile time you will see the correct type. Then you must create an instance of this type, for example:
var type = results.CompiledAssembly.GetType("CodeInjection.DynConcatenateString"); dynamic instance = Activator.CreateInstance(type);
There is no hope for C # for syntax B, since the CodeInjection.DynConcatenateString does not exist at compile time, then this line will not be executed.
If you must keep it static, then all you can do is use reflection to invoke this method ( dynamic then useless). Donβt worry about performance ... DLR is not much faster than simple Reflection (like AFAIK, as implemented with a touch of caching).
source share