Passing anonymous type as method parameters

2019-03-23 10:35发布

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).

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;
  }

An example usage:

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 });

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

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.

Thanks Ben

[Update]

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);

9条回答
爷、活的狠高调
2楼-- · 2019-03-23 10:55

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

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.

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.

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).

查看更多
混吃等死
3楼-- · 2019-03-23 10:55
public static void ExAnonymousType()
{
    var nguoi = new { Ten = "Vinh", Tuoi = 20 };
    Console.WriteLine(nguoi.Ten + " " + nguoi.Tuoi);
    DoSomeThing(nguoi);

}

private static void DoSomeThing(object nguoi)
{
    Console.WriteLine(nguoi.GetType().GetProperty("Ten").GetValue(nguoi,null));
}
查看更多
够拽才男人
4楼-- · 2019-03-23 10:56

Hi I have written a post about it:

http://blog.jorgef.net/2011/06/converting-any-object-to-dynamic.html

I hope that helps.

查看更多
叛逆
5楼-- · 2019-03-23 11:04

I did this once. What you can do is get the parameters expected from the function through reflection. Then, you can build up your array of parameters by matching the names in the array of parameters with the keys of the anonymous object.

Hope that helps :-).

查看更多
等我变得足够好
6楼-- · 2019-03-23 11:04

First of all, check System.Addin namespace, you might get some help there.

Second, you can create an interface of your own with specific method name and parameters, and let the plugin implement the interface. You can define plugin interface in a different project that can be referenced in both application as well as plugin project.

查看更多
Root(大扎)
7楼-- · 2019-03-23 11:07

Use dynamic object for parameters if you want to pass an anonymous type. The execute method of a plugin should expect certain properties of a parameter object in order to work. By using dynamic keyword C# compiler will be instructed to not perform type check on a parameter and will allow to use strongly-typed syntax in the plugin code. The properties name resolution will happen in run-time and if a passed object did not have such properties an exception will be thrown.

var o = new { FirstName = "John", LastName = "Doe" };

var result = MyMethod(o);

string MyMethod(dynamic o)
{
    return o.FirstName + " " + o.LastName;
}

Read more in this blog post

查看更多
登录 后发表回答