传递匿名类型作为方法参数 [英] Passing anonymous type as method parameters

查看:284
本文介绍了传递匿名类型作为方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的插件体系结构中,我当前正在将插件名称(字符串),方法名称(字符串)和参数(对象数组)传递给我的插件服务,以执行指定的方法并返回结果(类型T).

In my plugin architecture I am currently passing a plugin name (string), method name (string) and parameters (object array) to my plugin service to execute the specified method and return the result (of type T).

插件服务的execute方法如下:

The plugin service's execute method can be seen below:

public TResult Execute<TResult>(string pluginName, string operation, params object[] input) {
    MethodInfo method = null;
    TResult result = default(TResult);

    var plugin = _plugins.Enabled().FirstOrDefault(x => x.GetType().Name.Equals(pluginName,  StringComparison.InvariantCultureIgnoreCase));

    if (plugin != null) {
        method = plugin.GetType().GetMethods().FirstOrDefault(x => x.Name == operation);
        if (method != null) {
            result = (TResult)method.Invoke(plugin, input);
        }
    }
    return result;
  }

用法示例:

var url = AppHelper.PluginService.Execute<string>(
    "ImagePlugin",
    "GetImageUrl",
    new object[] { image, size });

我希望做的是改为传递匿名类型(因为我认为这更容易理解),即

What I would rather do is pass in an anonymous type instead (as I think this is more readable) i.e.

var url = AppHelper.PluginService.Execute<string>(
    "ImagePlugin",
    "GetImageUrl",
    new { image = image, targetSize = size });

如何更改Execute方法以将匿名类型属性映射到插件方法参数?

How would I change my Execute method to map the anonymous type properties to my plugin method parameters?

我曾考虑过在.net 4.0中使用新的动态类型,但我更喜欢在plugin方法上定义参数,而不是接受一个动态对象.

I had considered using the new dynamic type in .net 4.0 but I prefer to define my parameters on the plugin method rather than accepting one dynamic object.

谢谢 本

[更新]

浏览ASP.NET MVC源代码后,将匿名类型放入对象字典中似乎很简单,例如RouteValueDictionary. 借助反射,将动态创建linq表达式.尽管它是一个很好的实现,但我并不是真的想要所有这些额外的复杂性.

After looking through the ASP.NET MVC source code it seems simple enough to pull the anonymous type into an object dictionary e.g. RouteValueDictionary. With the help of reflection a linq expression is created dynamically. Although its a good implementation, I didn't really want all this extra complexity.

根据下面的评论,仅通过内联指定参数即可实现可读性(无需对象数组声明):

As per the comment below, I can achieve readability just by specifying my parameters inline (no need for the object array declaration):

var url = AppHelper.PluginService.Execute<string>("ImagePlugin", "GetImageUrl", image, size);

推荐答案

尽管我不建议其中任何一种,但仍有一些方法可以实现这一点.

There are some ways to make this possible although I wouldn't advice any of them.

首先,您可以使用反射,这意味着您必须在PluginService.Execute方法中编写很多其他(容易出错的)代码,才能获得所需的值.

First, you can use reflection which means you have to write a lot of additional (error-prone) code in your PluginService.Execute method to get the values you want.

第二,如果您知道要传递给方法的匿名类型的参数,则可以使用此处.您可以将方法强制转换为具有相同属性的方法中的另一个匿名类型. 此处是Jon Skeet对相同技术的另一种描述.

Second, if you know the parameters of the anonymous type you are passing to your method you can use the technique described here. You can cast to another anonymous type inside your method that has the same properties. Here is another description of the same technique from Jon Skeet.

第三,您可以使用 System.ComponentModel .例如,ASP.NET MVC使用此方法.它在引擎盖下使用反射.但是,在ASP.NET MVC中,属性名称是众所周知的(例如,controlleraction),或者它们的名称无关紧要,因为它们是按原样传递给控制器​​方法的(例如,id ).

Third, you can use classes from the System.ComponentModel. For example, ASP.NET MVC uses this. It uses reflection under the hood. However, in ASP.NET MVC either the property names are well-known (controller and action for example) or their names don't matter because they are passed as-is to a controller method (id for example).

这篇关于传递匿名类型作为方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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