在函数中动态创建或设置函数的返回类型 [英] Dynamically create or set the return type of a function in the function

查看:61
本文介绍了在函数中动态创建或设置函数的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在运行时确定返回类型的函数.我知道我可以返回objectdynamic,但是我的目标是返回类型化的object,并由编译器和InteliSense识别.

我知道我可以在返回object之后转换我的object,这是实现此目标最容易的事情,但这不是编程精神.

这是我尝试创建的示例

注意:我实际上并不希望按钮和网格...仅用于此示例.

Type T;
public T Question(bool aBool)
{
    if (aBool)
    {
        T = typeof(Button);
        return new Button();
    }
    else
    {
        T = typeof(Grid);
        return new Grid();
    }
}

现在,这显然行不通,我明白原因.但是我想知道是否有人可以使用这种方法,或者在当前的C#状态下这是不可能的.

对评论的回应...我知道这看起来像是魔术",而且我确实知道编译器必须弄清楚我要执行的结果.编译器/Intellisense/Visual Studio已经完成了许多其他操作.这些事情很简单,例如检测无法访问的代码或绘制视觉预览.我很好奇这是否是一项已实现的功能.

解决方案

此类方法的使用者实际上唯一依赖于返回类型是动态的事实的唯一可能方法是,至少针对该方法调用,则返回类型在编译时是静态已知的.

该方法有一个特定功能,该功能在编写该方法时具有某种未知类型,但在对该方法进行特定调用时已修复.该功能称为泛型".

public T Foo<T>()
    where T : new()
{
   return new T();
}

对于真正的动态返回类型,这确实是唯一可用的选项,它有很大的潜力可能真正有用.

如果这不是您想要的,或者这对您而言不是一个可行的选择,那么您的方法不应具有动态更改的返回类型的可能性很大.取而代之的是,它应该具有某种固定的返回类型,这种返回类型可以是一些更通用的类型,可以具有多个实现.通常,这将意味着一个接口,您可以向其返回任意数量的可能实现中的一个.如果调用者不需要真正知道或关心实现是什么,则应该执行此操作,而实际上,他们所需要知道的只是为他们提供了某种接口的实现,该接口公开了所有需要的东西.在您的情况下,如果调用方只需要知道给定了某种控制类型,并且Control的API提供了与之相关的所有功能,则Control之类的方法可能是可行的. /p>

I am attempting to create a function where the return type is determined at run-time. I know I could just return an object or dynamic, however my aim is to have the typed object returned, and be recognized by the compiler and InteliSense.

I know I could cast my object after it has been returned and that would be the easiest thing to do to implement this, but that is just not the programming spirit.

Here is an example of what I'm trying to create

Note: I do not actually want Buttons and Grids... that is just for this example.

Type T;
public T Question(bool aBool)
{
    if (aBool)
    {
        T = typeof(Button);
        return new Button();
    }
    else
    {
        T = typeof(Grid);
        return new Grid();
    }
}

Now, this obviously doesn't work and I understand why. But I want to know if anyone has a way that does work, or if this is not possible with the current state of C#.

Edit: A response to comments... I understand this would seem like "magic", and I do understand that the compiler will have to figure out what my result is for this to work. The compiler/Intellisense/Visual Studio already does this for many other things. While these things are can simple like detecting unreachable code, or drawing visual previews. I am curious if this is an implemented feature.

解决方案

The only possible way for the consumer of such a method to actually rely on the fact that the return type is dynamic is if, at least for that one method call, the return type is statically known at compile time.

There is a specific feature for a method that has some type unknown at the time the method is written, but fixed when a particular call to that method is make. That feature is called "generics".

public T Foo<T>()
    where T : new()
{
   return new T();
}

That's really the only available option for a truly dynamic return type that has much potential for really being useful.

If that's not what you want, or that is not a workable option for you, then odds are pretty high your method shouldn't have a dynamically changing return type. Instead it should have a fixed return type of some more generalized type that can have multiple implementations. Generally this would mean an interface, to which you can return one of any number of possible implementations. This should be done if the caller doesn't need to really know or care what the implementation is, but rather all they need to know is that they are given some implementation of an interface that exposes all of what they need. In your case, perhaps something like Control would be workable, if the caller only need to know that they are given some type of control, and to which the API of Control provides everything that they need to do with it.

这篇关于在函数中动态创建或设置函数的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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