DLR return type
-
Tuesday, September 01, 2009 1:06 AMhi. I need some DLR help. I am implementing an IDynamicMetaObjectProvider and DynamicMetaObject but I am having some issues getting the expected return type. I am overiding BindInvokeMember in the metaobject, I can see all the args types but no return type. Anyone know how I get to it if possible? I know the return type is dynamic but what if the thing you are invoking is dependent on a return type. I don't know which action to create in the DynamicMetaObject unless I know the return type the consumer is expecting.
- Moved by Karel ZikmundMicrosoft Employee Tuesday, December 22, 2009 6:41 PM CLR (From:.NET Framework 4 Beta 2 – General)
All Replies
-
Tuesday, September 01, 2009 2:45 AMOk I see since dynamic object methods returns an dynamic which is an object so I guess there needs to be some conversion. I see compiler generates some code to do the conversion. I guess my question now is can I intercept the Convert?
I tried overriding BindConvert in my DynamicMetaObject but it doesnt get hit.
I am calling some external stuff and I need to know what the return type is before I can create the expression to invoke :(
object x = new DynamicTest();
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, "TestMethod", 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))); -
Friday, September 04, 2009 4:44 AMModerator
InvokeMember operation return type is always Object - see InvokeMemberBinder.ReturnType property. Can you be more specific about how your BindInvokeMember depends on the return type?
The dynamic conversion that is applied by C# in the above code is performed on the object that you return from InvokeMember. You can control the conversion by returning an implementation of IDynamicMetaObjectProvider whose meta-object responds to Convert operation. -
Friday, September 04, 2009 7:09 PMI tried overriding BindConvert in my DynamicMetaObject but it doesnt get hit.
-
Saturday, September 05, 2009 3:26 AMModerator
Can you include the source code of BindInvokeMember or its part that constructs the return value?
-
Wednesday, September 09, 2009 2:28 AM
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 override DynamicMetaObject BindConvert(ConvertBinder binder) { return base.BindConvert(binder); } public static int Test() { return 10; } } public class TestDynamicObject : IDynamicMetaObjectProvider { DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter) { return new TestDynamicMetaObject(parameter, this); } }and being called
static void Main(string[] args) { try { dynamic x = new TestDynamicObject(); object gg= x.Test(); Console.WriteLine(gg); } catch (Exception excep) { Console.WriteLine(excep); } Console.ReadLine(); }Asked the same question here too http://stackoverflow.com/questions/1360097/dlr-return-type
-
Wednesday, September 09, 2009 6:34 PMModerator
I talked to Dino who was answering you on StackOverflow and we came up with an answer for you there. If you have any further questions on this subject, please ask on StackOverflow (http://stackoverflow.com/questions/1360097/dlr-return-type) so that we don't need to duplicate our answers here.
- Edited by Karel ZikmundMicrosoft Employee Monday, October 19, 2009 9:29 PM Add StackOverflow link
- Marked As Answer by Karel ZikmundMicrosoft Employee Monday, October 19, 2009 9:29 PM
- Unmarked As Answer by AbdElRaheim Friday, December 11, 2009 9:23 AM
-
Friday, December 11, 2009 9:26 AM
Yeah he never answer my question. It appears the return type at least is the beta i was using is not know...
I was trying to make a p/invoke example using the DLR but it seems like this is not possible unless you pass in the expected return type as a parameter which I ended up doing :\ This seems to me like a limitation... Hopefully it will be addressed in the future. -
Friday, July 02, 2010 10:22 AM
Yeah he never answer my question. It appears the return type at least is the beta i was using is not know...
I was trying to make a p/invoke example using the DLR but it seems like this is not help possible unless you pass in the expected return type as a parameter which I ended up doing :\ This seems to me like a limitation... Hopefully it will be addressed in the future.
Have you got the answer as so long has past?

