System.Delegate.CreateDelegate()

作者:追风剑情 发布于:2015-3-5 17:25 分类:C#

将对象中的方法映射成委托

using System;

namespace CreateDelegateTest {
    class Program {

        public delegate void DelegateDisplay( string s );

        static void Main( string[] args ) {
            TestClass test = new TestClass();
            //将test对象中的Display(string s)映射成委托对象
            DelegateDisplay display = ( DelegateDisplay )System.Convert.ChangeType( System.Delegate.CreateDelegate( typeof( DelegateDisplay ), test, "Display" ), typeof( DelegateDisplay ) );
            display("test!!!!");

            Console.Read();
        }
    }

    class TestClass {
        public void Display(string s) {
            Console.WriteLine(s);
        }
    }
}

createDeleagate.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号