Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 您将如何编写一个采用[ModelBinder(typeof(GeoPointModelBinder))]这样的参数的方法_C# - Fatal编程技术网

C# 您将如何编写一个采用[ModelBinder(typeof(GeoPointModelBinder))]这样的参数的方法

C# 您将如何编写一个采用[ModelBinder(typeof(GeoPointModelBinder))]这样的参数的方法,c#,C#,我正在阅读一个关于ASP.NET Web API框架的示例,以及如何在请求期间创建modelbinder来绑定参数 我的问题是,如何编写一个c#类/方法来接受如下参数: public HttpResponseMessage Get([ModelBinder(typeof(GeoPointModelBinder))] GeoPoint location) 如果你能简单地解释一下语法和它背后的基本思想,那就太好了 参考资料:线路已100%完工。ModelBinder-ModelBinderAttr

我正在阅读一个关于ASP.NET Web API框架的示例,以及如何在请求期间创建modelbinder来绑定参数

我的问题是,如何编写一个c#类/方法来接受如下参数:

public HttpResponseMessage Get([ModelBinder(typeof(GeoPointModelBinder))] GeoPoint location)
如果你能简单地解释一下语法和它背后的基本思想,那就太好了


参考资料:

线路已100%完工。ModelBinder-ModelBinderAttribute类(),必须创建的GeoPointModelBinder-binder类,GeoPoint-model类。ModelBinderAttribute类具有获取类型的构造函数。您在那里传递typeof(gemodelbinder)。@deru Meister方括号呢?它是一个属性。属性可以设置为类、属性、字段甚至参数<代码>[SomeCoole(123)]字符串值123是默认值吗?不是,它是传递给属性的值。F.e.HttpActionName(“CustomName”)属性可在ASP.NET MVC控制器中用于为方法设置不同的操作名称。
using System;
using System.Reflection;

namespace Test081204
{
    [AttributeUsage(AttributeTargets.Parameter)]
    public class SomeCoolAttribute : System.Attribute
    {
        public readonly int Val;

        public SomeCoolAttribute(int val)
        {
            Val = val;
        }
    }

    class Test
    {
        public void Run([SomeCool(123)] string value)
        {
            // Prints "In Run: test123"
            Console.WriteLine("In Run: " + value);
        }
    }

    class Program
    {
        public static void Main()
        {
            var parameters = typeof(Test).GetMethod("Run").GetParameters();
            var attr = parameters[0].GetCustomAttribute(typeof(SomeCoolAttribute)) as SomeCoolAttribute;

            // Prints "123"
            Console.WriteLine("In Main: " + attr.Val);

            new Test().Run("test" + attr.Val.ToString());
        }
    }
}