English 中文(简体)
Understanding func<T, TResult> C#
原标题:understanding func<T, TResult> C#
  • 时间:2010-04-18 00:47:32
  •  标签:
  • c#-3.0

I am trying to refactor some of my code to use Func I have created a sample to try to understand how it works but I am getting compilation error. In the e.g. below when I am trying to access the parameter attributes or return the return value the compiler complain.

请澄清。

using System;

namespace chsarp
{
    class Program
    {
        static void Main(string[] args)
        {
            ParamInput input = new ParamInput();
            input.ReservationID = 10;
            Test testMethod = new Test();
            Func<ParamInput, ReservationResult> methodToInvoke = testMethod.GetStatus;
            ReservationResult result = TestFn(methodToInvoke, input);
        }

        static Result TestFn<Param, Result>(Func<Param, Result> methodToInvoke, Param parameter) where Result : new()
        {
            Result result = new Result();
            try
            {
                result = methodToInvoke(parameter);
            }
            catch (Exception exception)
            {
                result.status = string.Format("Error-{0} during reserving {1}",
                                                parameter.ReservationID,
                                                exception.Message);
            }
            return result;
        }
    }

    class Test
    {
        public ReservationResult GetStatus(ParamInput msg)
        {
            ReservationResult result = new ReservationResult();
            result.status = string.Format("The ReservationID-{0}", msg.ReservationID);
            return result;

        }
    }

    class ParamInput
    {
        public int ReservationID { get; set; }
    }

    class ReservationResult
    {
        public string status { get; set; }
    }

}
问题回答

error CS1061:  Result  does not contain a definition for  status 
error CS1061:  Param  does not contain a definition for  ReservationID 

只有在汇编者知道普通类成员的情况下,使用普通类观点的守则才能汇编成册。 它不知道结果类型论点有“地位”成员。 如果你援引<条码> 试验Fn<object, Object>,这肯定是正确的。

你们需要利用制约因素来开展这项工作。 你已经这样做,但新的()实力不够。 例如,“结果:结果”等,IResult是一个具有地位财产的接口。 或基类。 汇编者现在可以百分之百地确定,任何允许的通用方法的具体事例都不会造成时间错误。 由于只有在以具有“地位”财产的类型援引时才能对其进行汇编。

所有这些都同样适用于Param类的论点。 当你这样做时,通用方法的效用就会迅速消失,这不是适当的使用。 除非你能够发挥我的作用,否则,正如你一样,我能说几句。

......

你们应该去掉你在你们的提问中的目标。

见汉斯对可能有助于了解错误的细节的答复。

你们的问题是,你再说,这一功能应当产生一种可观的结果,但你并没有说,它必须拥有一个称为“状态”的财产。

namespace chsarp
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, string> m = Test.GetStatus;
            var t = TestFn(m, 10);
        }

        static string TestFn<TParam>(Func<TParam, string> m, TParam p)
        {
            try { return m(p); }
            catch (Exception exception)
            {
                return string.Format("Reserving "{0}" failure exception: {1}", p, exception);
            }
        }
    }

    static class Test
    {
        public static string GetStatus(int inp)
        {
            return string.Format("The ReservationID-{0}", inp);
        }
    }
}




相关问题
Linq - 2 tables - Sum

Asiento ******** Id_Asiento integer key Fecha date It_Asiento ********** Id_Asiento integer Forenkey Importe float I wan to do This SQL Query with Linq select Asiento.Id_Asiento, ...

Threading and un-safe variables

I have code listed here: Threading and Sockets. The answer to that question was to modify isListening with volatile. As I remarked, that modifier allowed me to access the variable from another thread....

How to cancel an asynchronous call?

How to cancel an asynchronous call? The .NET APM doesn t seem to support this operation. I have the following loop in my code which spawns multiple threads on the ThreadPool. When I click a button on ...

热门标签