本文介绍了DLR返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助DLR。我采取的IDynamicMetaObjectProvider和DynamicMetaObject但我有一些问题得到预期的返回类型。我在元对象overiding BindInvokeMember,我可以看到所有的ARGS类型,但没有返回类型。任何人都知道,如果可能,我怎么得到它?我知道返回类型是动态的,但如果你调用的是依赖于返回类型是什么。我不知道在DynamicMetaObject要执行的操作,除非我知道返回类型消费者的期待。



更新两个



我不能粘贴在这里我实际的代码,因为它要求各类工作的东西。一些示例动态对象代码如下。

 公共类TestDynamicMetaObject:DynamicMetaObject 
{
公共TestDynamicMetaObject(表达表达,对象的值)$ B $ A:基地(表达式,BindingRestrictions.Empty,值)
{
}

公众覆盖DynamicMetaObject BindInvokeMember(InvokeMemberBinder粘合剂,DynamicMetaObject []参数)
{
委托方法=新Func键与LT; INT>(试验);
返回新DynamicMetaObject(
Expression.Call(method.Method),
BindingRestrictions.GetInstanceRestriction(表达式,值),

);
}

公共静态INT测试()
{
返回10;
}

}
公共类TestDynamicObject:IDynamicMetaObjectProvider
{
DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression参数)
{
返回新TestDynamicMetaObject(参数,这一点);
}
}

下面就是我使用。



 静态无效的主要(字串[] args)
{

{
动态点¯x =新TestDynamicObject();
INT GG = x.Test();
Console.WriteLine(GG);
}
赶上(例外excep)
{
Console.WriteLine(excep);
}
到Console.ReadLine();
}

下面是编译器产生的代码。

 私有静态无效的主要(字串[] args)
{

{
对象x =新TestDynamicObject();
如果(lt;主> o__SiteContainer0<> p__Site1 == NULL)
{
<主方式> o__SiteContainer0<> p__Site1 =调用点< Func键<调用点,对象, INT>> .Create(新CSharpConvertBinder(typeof运算(INT),CSharpConversionKind.ImplicitConversion,FALSE));
}
如果(小于主要方式> o__SiteContainer0<> p__Site2 == NULL)
{
<主方式> o__SiteContainer0<> p__Site2 =调用点< Func键< ;调用点,对象,对象>> .Create(新CSharpInvokeMemberBinder(CSharpCallFlags.None,测试的typeof(程序),空,新CSharpArgumentInfo [] {新CSharpArgumentInfo(CSharpArgumentInfoFlags.None,NULL)}));
}
Console.WriteLine(lt;主> o__SiteContainer0<> p__Site1.Target(lt;主> o__SiteContainer0<> p__Site1,<主> o__SiteContainer0<> p__Site2 .TARGET(<主方式> o__SiteContainer0&所述;> p__Site2中,x)));
}
赶上(例外excep)
{
Console.WriteLine(excep);
}
到Console.ReadLine();
}


解决方案

有关标准二进制其回报一些返回类型几乎总是反对(获取,设置,操作,等等)。否则,它是无效的标准绑定(例如DeleteMember)。



您还可以得到在从传入粘结剂返回类型物业运行时的预期收益类型。


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 perform in the DynamicMetaObject unless I know the return type the consumer is expecting.

Update Two

I cant paste my actual code here since it calls all kinds of work stuff. Some sample dynamic object code is below.

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 is where I am using.

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();
}
解决方案

For the standard binaries which return something the return type is almost always object (get, set, operations, etc...). Otherwise it's void for the standard bindings (e.g. DeleteMember).

You can also get the expected return type at runtime from the ReturnType property on the incoming binder.

这篇关于DLR返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:36