You can't use dynamic
directly in this scenario. dynamic
always requires a class instance, but you are trying to call a method in a static class, hence you don't have a class instance.
However, you can create a helper class and use that in combination with dynamic
:
public class StaticMethodInvoker : DynamicObject
{
Type _containingType;
public StaticMethodInvoker(Type containingType)
{
_containingType = containingType;
}
public override bool TryInvokeMember(
InvokeMemberBinder binder, Object[] args, out Object result)
{
result = _containingType.InvokeMember
binder.Name,
BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public,
null, null, args);
return true;
}
}
Usage:
var type = results.CompiledAssembly.GetType("CodeInjection.DynConcatenateString");
dynamic DynConcatenateString = new StaticMethodInvoker(type);
string result = DynConcatenateString.Concatenate(s1, s2);