(原創)C#反射知識分享之二

來源:互聯網
上載者:User

本人來源於我在學習JAVA的反射過程中,想瞭解一下C#的反射過程,所以寫了一下知識總結,希望能為後來者的一些協助

·         3 使用 MethodInfo 發現以下資訊:方法的名稱、傳回型別、參數、存取修飾詞(如 public或 private)和實現詳細資料(如 abstract或 virtual)等。使用 Type 的 GetMethods 或 GetMethod 方法來調用特定的方法。

Example3 :   Assembly assem1 = Assembly.Load("ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");            Assembly assem = Assembly.LoadFrom("ClassLibrary1.dll");            Console.Write(assem.FullName);            AssemblyName assemName = assem.GetName();            Console.WriteLine("\nName: {0}", assemName.Name);            Console.WriteLine("Version: {0}.{1}",                assemName.Version.Major, assemName.Version.Minor);            int i,j;            Type[] types = assem.GetTypes();                     for (i = 0; i < types.GetLength(0); ++i)            {                Console.WriteLine(types[i].Name);            }          Type mytype = types[0];      MethodInfo[] methods=mytype.GetMethods();            for (i = 0; i < methods.Length; i++)            {                if (methods[i].MemberType == MemberTypes.Method)                {                    Console.WriteLine("---------------");                    Console.WriteLine("method'name        : {0}", methods[i].Name);                    Console.WriteLine("Public        : {0}", methods[i].IsPublic);                    Console.WriteLine("Private        : {0}", methods[i].IsPrivate);                    Console.WriteLine("Static        : {0}", methods[i].IsStatic);                    Console.WriteLine("Abstract        : {0}", methods[i].IsAbstract);                    Console.WriteLine("IsVirtual        : {0}", methods[i].IsVirtual);                    Console.WriteLine("傳回型別¨ª        : {0}", methods[i].ReturnType.ToString());                    foreach (ParameterInfo pi in ((MethodInfo)methods[i]).GetParameters())                    {                        Console.WriteLine(" 參數 Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name);                    }                    Console.WriteLine("---------------");                }            }

  

列印結果如下:

………….

---------------

method'name        : run

Public        : True

Private        : False

Static        : False

Abstract        : False

IsVirtual        : False

傳回型別       : System.Void

---------------

---------------

method'name        : myReturnMethod

Public        : True

Private        : False

Static        : False

Abstract        : False

IsVirtual        : False

傳回型別       : System.String

 參數 Parameter: Type=System.String, Name=abc

 參數 Parameter: Type=System.Int32, Name=i

---------------

---------------

method'name        : myReturnMethod

Public        : True

Private        : False

Static        : False

Abstract        : False

IsVirtual        : False

傳回型別        : System.String

 參數 Parameter: Type=System.String, Name=abc

 參數 Parameter: Type=System.Int32, Name=i

 參數 Parameter: Type=System.String, Name=a

---------------

 

4.使用ConstructorInfo瞭解建構函式的名稱、參數、存取修飾詞(如pulic 或private)和實現詳細資料(如abstract或virtual)等。使用Type的GetConstructors或GetConstructor方法來調用特定的建構函式。 

Example4:

Assembly assem1 = Assembly.Load("ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");            Assembly assem = Assembly.LoadFrom("ClassLibrary1.dll");            Console.Write(assem.FullName);            AssemblyName assemName = assem.GetName();            Console.WriteLine("\nName: {0}", assemName.Name);            Console.WriteLine("Version: {0}.{1}",                assemName.Version.Major, assemName.Version.Minor);            int i,j;            Type[] types = assem.GetTypes();Type mytype = types[0];ConstructorInfo constructorInfoObj = mytype.GetConstructor(                BindingFlags.Instance | BindingFlags.Public, null,                CallingConventions.HasThis, types, null);if(constructorInfoObj != null)            {                              Console.WriteLine(constructorInfoObj.ToString());            }            else            {                Console.WriteLine("is not available.");            }

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.