I need help DLR. I implement IDynamicMetaObjectProvider and DynamicMetaObject, but I am having some problems getting the expected return type. I surpass BindInvokeMember in a meta object, I see all args types, but it does not return a type. Does anyone know how I will get to him, if possible? I know that the return type is dynamic, but what if the thing you call depends on the return type. I do not know what action needs to be performed in DynamicMetaObject if I do not know the type of return that the consumer expects.
Update two
I canβt insert my actual code here, as it calls all kinds of work. The following is an example of dynamic object code.
public class TestDynamicMetaObject : DynamicMetaObject { public TestDynamicMetaObject(Expression expression, object value) : base (expression, BindingRestrictions.Empty, value) { } public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args) { Delegate method = new Func<int>(Test); return new DynamicMetaObject( Expression.Call(method.Method), BindingRestrictions.GetInstanceRestriction(Expression,Value), Value ); } public static int Test() { return 10; } } public class TestDynamicObject : IDynamicMetaObjectProvider { DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter) { return new TestDynamicMetaObject(parameter, this); } }
Here I use.
static void Main(string[] args) { try { dynamic x = new TestDynamicObject(); int gg= x.Test(); Console.WriteLine(gg); } catch (Exception excep) { Console.WriteLine(excep); } Console.ReadLine(); }
Here is the code that the compiler creates.
private static void Main(string[] args) { try { object x = new TestDynamicObject(); if (<Main>o__SiteContainer0.<>p__Site1 == null) { <Main>o__SiteContainer0.<>p__Site1 = CallSite<Func<CallSite, object, int>>.Create(new CSharpConvertBinder(typeof(int), CSharpConversionKind.ImplicitConversion, false)); } if (<Main>o__SiteContainer0.<>p__Site2 == null) { <Main>o__SiteContainer0.<>p__Site2 = CallSite<Func<CallSite, object, object>>.Create(new CSharpInvokeMemberBinder(CSharpCallFlags.None, "Test", typeof(Program), null, new CSharpArgumentInfo[] { new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null) })); } Console.WriteLine(<Main>o__SiteContainer0.<>p__Site1.Target(<Main>o__SiteContainer0.<>p__Site1, <Main>o__SiteContainer0.<>p__Site2.Target(<Main>o__SiteContainer0.<>p__Site2, x))); } catch (Exception excep) { Console.WriteLine(excep); } Console.ReadLine(); }
c # dynamic-binding dynamic-language-runtime
AbdElRaheim Sep 01 '09 at 0:55 2009-09-01 00:55
source share