尽管签名中有返回类型,为什么此方法仍保持动态返回? [英] Why does this method keep returning dynamic despite the return type in the signature?

查看:79
本文介绍了尽管签名中有返回类型,为什么此方法仍保持动态返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,激活器(此处未显示)返回的类型只是我创建的POCO。没什么特别的。但是尽管如此,返回的对象 GetWrapper GetWrapper< T> 的返回类型是动态类型,因此没有智能感知可以看到返回的实际类的属性。有任何想法吗?

So the type being returned by the activator (not shown here) is just a POCO that I created. Nothing special about it at all. But despite, the return type of GetWrapper or GetWrapper<T> the object being returned is of type dynamic so none of the properties of the actual class being return are being seen by intellisense. Any ideas?

更新:
对不起,乔恩:

UPDATE: Sorry, Jon:

public static T GetWrapper<T>(object type, dynamic obj)
   where T : class, IRedditObject
{
    return GetWrapper(type, obj) as T;
}

public static object GetWrapper(object type, dynamic obj)
{
    return Activator.CreateInstance(Types[type.ToString()], obj);
}

public class LinkWrapper : IRedditObject
{
    private readonly dynamic _link;

    public string Kind { get { return "t3"; } }
    public dynamic Data { get { return _link.data; } }

    public LinkWrapper(dynamic link)
    {
        _link = link;
    }

    public string Domain { get { return Data.domain; } }
}

[TestMethod]
public void Test()
{
    var proxy = new SubredditProxy();
    var dotnet = proxy.GetSubredditAsync("dotnet").Result;

    var child1 = dotnet.data.children[0];

    // this is being returned as dynamic
    var obj = RedditDynamicObjectWrapperFactory.GetWrapper<LinkWrapper>(child1.kind, child1);


    Assert.IsNotNull(obj);
}


推荐答案

我强烈怀疑code> child1 或 child1.kind 的类型为 dynamic ,这意味着

I strongly suspect that either child1 or child1.kind are of type dynamic, meaning that the expression is deemed to be a dynamically-bound expression, despite everything else.

下面是一个简短但完整的示例,用于说明我的意思: b

Here's a short but complete example to demonstrate what I mean:

using System;

class Test
{
    public static T Foo<T>(object x)
    {
       return default(T);
    }

    public static void Main(string[] args)
    {
        object a = new object();
        dynamic b = a;

        var c = Foo<string>(a);
        var d = Foo<string>(b);

        Console.WriteLine(c.SomeRandomMember); // Invalid
        Console.WriteLine(d.SomeRandomMember); // Valid
    }
}

无效的语句是无效的,因为类型 c 的值为 string -但随后的语句很好,因为 d 是动态的

The invalid statement is invalid because the type of c is string - but the subsequent statement is fine, because the type of d is dynamic.

即使只有一种可能的方法, 在执行时被绑定-尽管该绑定将始终有效-基本规则是几乎 any 表达式都包含 dynamic 值被认为是动态类型。 (有一些例外,例如 as new 。)

Even though there's only one possible method which it could be bound to at execution time - and even though that binding will always work - the basic rule is that almost any expression involving a dynamic value is deemed to be of type dynamic. (There are a few exceptions such as as and new.)

要使返回值不动态,只需将值转换为 object

To make your return value non-dynamic, just cast your values to object:

var obj = RedditDynamicObjectWrapperFactory.GetWrapper<LinkWrapper>
     ((object) child1.kind, (object) child1);

现在将是静态绑定的呼叫。或者,您可以将其保留为动态绑定调用,并使用从 dynamic 类型的表达式到其他类型的隐式转换:

That will now be a statically bound call. Or you could leave it as a dynamically bound call, and use the implicit conversion from an expression of type dynamic to other types:

LinkWrapper obj = RedditDynamicObjectWrapperFactory.GetWrapper<LinkWrapper>(child1.kind, child1);

这篇关于尽管签名中有返回类型,为什么此方法仍保持动态返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆