C# Func<T,TResult>

简介:
using System;

namespace FuncDemo
{ 
    internal class Program
    {
        private static void Main()
        {
            //类似委托功能
            Func<InputArgs, Result> func = TsetFunction;
            Console.WriteLine("第一种方式:");
            Console.WriteLine(func(new InputArgs("zhangqs008", "123456")));

            Console.WriteLine("=============================================");

            Console.WriteLine("第二种方式:");
            CallMethod(func, new InputArgs("zhangqs008", "1234567")); //或者 
            CallMethod(TsetFunction, new InputArgs("zhangqs008", "1234567"));

            Console.Read();
        }


        public static Result TsetFunction(InputArgs input)
        {
            Result result = new Result();
            result.Flag = String.Compare("zhangqs008", input.UserName, StringComparison.OrdinalIgnoreCase) == 0 &
                String.Compare("123456", input.Password, StringComparison.OrdinalIgnoreCase) == 0;
            result.Msg = "当前调用者:" + input.UserName;
            return result;
        }

        public static void CallMethod<T>(Func<T, Result> func, T item)
        {
            Result result = func(item);
            Console.WriteLine(result.ToString()); 
        }
        /// <summary>
        /// 方法参数
        /// </summary>
        public class InputArgs
        {
            public InputArgs(string userName, string password)
            {
                UserName = userName;
                Password = password;
            }

            public string UserName { get; set; }
            public string Password { get; set; }
        }

        /// <summary>
        /// 方法结果
        /// </summary>
        public class Result
        {
            public string Msg { get; set; }
            public bool Flag { get; set; }
            public override string ToString()
            {
                return string.Format("Flag:{0},Msg:{1}", Flag, Msg);
            }
        }

    }
}

目录
相关文章
|
Linux 数据安全/隐私保护 Shell
|
存储 算法 C++
C++中的字符串操作&lt;cstring&gt;和&lt;string&gt;的区别
目录 目录 参考资料 字符串 cstring和string的区别在哪 string类的实现 注意不要盲目相信以下内容! 不要盲目相信以下内容! 不要盲目相信以下内容! (重要的事情说三遍),虽然以下内容也经过了我的验证,但是我的验证可能有错误的地方,欢迎大家留言告知。
1279 0
|
Java
Java集合转换【List&lt;--&gt;数组、List&lt;--&gt;Set、数组&lt;--&gt;Set、Map--&gt;Set、Map--&gt;List】
[java] view plaincopy //List--&gt;数组      List&lt;String&gt; list = new ArrayList&lt;String&gt;();      list.add("tom");      list.add("Jerval");      list.add("Wei
1357 0
Dev GridView 绑定List&lt;T&gt;、BindingList &lt;T&gt;、BindingSource
作者:jiankunking 出处:http://blog.csdn.net/jiankunking         今天听到同事处理数据结构的时候特意处理为了 BindingList&lt;T&gt;,据说可以直接绑定到Dev GridView上,于是测试了一下: 1、在Dev GridView中新增三列,三列的FieldName分别对应与FormItem类对应:ItemKey、Name
2448 0
错误 1 Files 的值“&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine”无效。路径中具有非法字符。
Microsoft Visual Studio 2010项目编译的时候,提示如下错误: 错误 1 Files 的值“&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine”无效。路径中具有非法字符。 解决办法:在你的工程OBJ/DEBUG目录下,找到 工程名.csproj.FileListAbsolute.txt的文件打开并删除含有'&lt;&lt;&lt;&lt;&lt;&l
2251 0
.Net——Func&lt;&gt;与Action&lt;&gt;
                    首先来先写几个测试函数: public delegate void SayHello(string strName); public static void Hello(string strName) { Console.
975 0
|
Oracle 关系型数据库 中间件
15-5-23 下午02时22分58秒 CST&gt; &lt;Info&gt; &lt;Management&gt; &lt;BEA-141281&gt; &lt;unable to get file lock, will retry ...&gt;
 A-141281&gt; &lt;unable to get file lock, will retry ...&gt;   http://gdutlzh.blog.163.com/blog/static/164746951201291903824812/I ran into this error the first time I restarted Weblogic on on
1719 0
|
移动开发 Java Apache
Unrecognised tag: 'encoding' (position: START_TAG seen ...&lt;/version&gt;\r\n\t\t\t\t&lt;encoding&gt;... @12:15
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/45620729 ...
2021 0
|
移动开发 Java Apache
Unrecognised tag: &#39;encoding&#39; (position: START_TAG seen ...&lt;/version&gt;\r\n\t\t\t\t&lt;encoding&gt;... @12:15
执行Maven Install打包的时候,出现以下错误信息: [INFO] Scanning for projects... [ERROR] The build could not read 1 project -> [Help 1] [ERROR] [ERROR] The project pro.
1228 0
实现string类的操作符重载 + = &gt; &lt; == != &gt;&gt; &lt;&lt;
<pre code_snippet_id="596931" snippet_file_name="blog_20150203_1_6205569" name="code" class="objc">//MyString.h #pragma once #include &lt;iostream&gt; using namespace std; class MyString { priva
1357 0