最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于C#反射 你需要知道的

 更新時間:2020年06月09日 14:30:15   作者:Liam Wang  
這篇文章主要介紹了C#反射的相關(guān)知識,文中講解的非常詳細,代碼幫助大家更好的參考學(xué)習(xí),感興趣的朋友可以了解下

通常,反射用于動態(tài)獲取對象的類型、屬性和方法等信息。今天帶你玩轉(zhuǎn)反射,來匯總一下反射的各種常見操作,撿漏看看有沒有你不知道的。

獲取類型的成員

Type 類的 GetMembers 方法用來獲取該類型的所有成員,包括方法和屬性,可通過 BindingFlags 標志來篩選這些成員。

using System;
using System.Reflection;
using System.Linq;

public class Program
{
  public static voidMain()
  {
    var members = typeof(object).GetMembers(BindingFlags.Public |
      BindingFlags.Static | BindingFlags.Instance);
    foreach (var member in members)
    {
      Console.WriteLine($"{member.Name} is a {member.MemberType}");
    }
  }
}

輸出:

GetType is a Method
GetHashCode is a Method
ToString is a Method
Equals is a Method
ReferenceEquals is a Method
.ctor is a Constructor

GetMembers 方法也可以不傳 BindingFlags,默認返回的是所有公開的成員。

獲取并調(diào)用對象的方法

Type 類型的 GetMethod 方法用來獲取該類型的 MethodInfo,然后可通過 MethodInfo 動態(tài)調(diào)用該方法。

對于非靜態(tài)方法,需要傳遞對應(yīng)的實例作為參數(shù),示例:

class Program
{
  public static void Main()
  {
    var str = "hello";
    var method = str.GetType()
      .GetMethod("Substring", new[] {typeof(int), typeof(int)});
    var result = method.Invoke(str, new object[] {0, 4}); // 相當于 str.Substring(0, 4)
    Console.WriteLine(result); // 輸出:hell
  }
}

對于靜態(tài)方法,則對象參數(shù)傳空,示例:

var method = typeof(Math).GetMethod("Exp");
// 相當于 Math.Exp(2)
var result = method.Invoke(null, new object[] {2});
Console.WriteLine(result); // 輸出(e^2):7.38905609893065

如果是泛型方法,則還需要通過泛型參數(shù)來創(chuàng)建泛型方法,示例:

class Program
{
  public static void Main()
  {
    // 反射調(diào)用泛型方法
    MethodInfo method1 = typeof(Sample).GetMethod("GenericMethod");
    MethodInfo generic1 = method1.MakeGenericMethod(typeof(string));
    generic1.Invoke(sample, null);

    // 反射調(diào)用靜態(tài)泛型方法
    MethodInfo method2 = typeof(Sample).GetMethod("StaticMethod");
    MethodInfo generic2 = method2.MakeGenericMethod(typeof(string));
    generic2.Invoke(null, null);
  }
}

public class Sample
{
  public void GenericMethod<T>()
  {
    //...
  }
  public static void StaticMethod<T>()
  {
    //...
  }
}

創(chuàng)建一個類型的實例

使用反射動態(tài)創(chuàng)建一個類型的實例有多種種方式。最簡單的一種是用 new() 條件聲明。

使用 new 條件聲明

如果在一個方法內(nèi)需要動態(tài)創(chuàng)建一個實例,可以直接使用 new 條件聲明,例如:

T GetInstance<T>() where T : new()
{
  T instance = newT();
  return instance;
}

但這種方式適用場景有限,比如不適用于構(gòu)造函數(shù)帶參數(shù)的類型。

使用 Activator 類

使用 Activator 類動態(tài)創(chuàng)建一個類的實例是最常見的做法,示例:

Type type = typeof(BigInteger);
object result = Activator.CreateInstance(type);
Console.WriteLine(result); // 輸出:0
result = Activator.CreateInstance(type, 123);
Console.WriteLine(result); // 輸出:123

動態(tài)創(chuàng)建泛類型實例,需要先創(chuàng)建開放泛型(如List<>),再根據(jù)泛型參數(shù)轉(zhuǎn)換為具象泛型(如List<string>),示例:

// 先創(chuàng)建開放泛型
Type openType = typeof(List<>);
// 再創(chuàng)建具象泛型
Type[] tArgs = { typeof(string) };
Type target = openType.MakeGenericType(tArgs);
// 最后創(chuàng)建泛型實例
List<string> result = (List<string>)Activator.CreateInstance(target);

如果你不知道什么是開放泛型和具象泛型,請看本文最后一節(jié)。

使用構(gòu)造器反射

也可以通過反射構(gòu)造器的方式動態(tài)創(chuàng)建類的實例,比上面使用 Activator 類要稍稍麻煩些,但性能要好些。示例:

ConstructorInfo c = typeof(T).GetConstructor(new[] { typeof(string) });
if (c == null)
  throw new InvalidOperationException("...");
T instance = (T)c.Invoke(new object[] { "test" });

使用 FormatterServices 類

如果你想創(chuàng)建某個類的實例的時候不執(zhí)行構(gòu)造函數(shù)和屬性初始化,可以使用 FormatterServices 的 GetUninitializedObject 方法。示例:

class Program
{
  static void Main()
  {
    MyClass instance = (MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass));
    Console.WriteLine(instance.MyProperty1); // 輸出:0
    Console.WriteLine(instance.MyProperty2); // 輸出:0
  }
}

public class MyClass
{
  public MyClass(int val)
  {
    MyProperty1 = val < 1 ? 1 : val;
  }

  public int MyProperty1 { get; }

  public int MyProperty2 { get; set; } = 2;
}

獲取屬性或方法的強類型委托

通過反射獲取到對象的屬性和方法后,如果你想通過強類型的方法來訪問或調(diào)用,可以在中間加一層委托。這樣的好處是有利于封裝,調(diào)用者可以明確的知道調(diào)用時需要傳什么參數(shù)。 比如下面這個方法,把 Math.Max 方法提取為一個強類型委托:

var tArgs = new Type[] { typeof(int), typeof(int) };
var maxMethod = typeof(Math).GetMethod("Max", tArgs);
var strongTypeDelegate = (Func<int, int, int>)Delegate
  .CreateDelegate(typeof(Func<int, int, int>), null, maxMethod);
Console.WriteLine("3 和 5 之間最大的是:{0}", strongTypeDelegate(3, 5)); // 輸出:5

這個技巧也適用于屬性,可以獲取強類型的 Getter 和 Setter。示例:

var theProperty = typeof(MyClass).GetProperty("MyIntProperty");

// 強類型 Getter
var theGetter = theProperty.GetGetMethod();
var strongTypeGetter = (Func<MyClass, int>)Delegate
  .CreateDelegate(typeof(Func<MyClass, int>), theGetter);
var intVal = strongTypeGetter(target); // 相關(guān)于:target.MyIntProperty

// 強類型 Setter
var theSetter = theProperty.GetSetMethod();
var strongTypeSetter = (Action<MyClass, int>)Delegate
  .CreateDelegate(typeof(Action<MyClass, int>), theSetter);
strongTypeSetter(target, 5); // 相當于:target.MyIntProperty = 5

反射獲取自定義特性

以下是四個常見的場景示例。

示例一,找出一個類中標注了某個自定義特性(比如 MyAtrribute)的屬性。

var props = type
  .GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
  .Where(prop =>Attribute.IsDefined(prop, typeof(MyAttribute)));

示例二,找出某個屬性的所有自定義特性。

var attributes = typeof(t).GetProperty("Name").GetCustomAttributes(false);

示例三,找出程序集所有標注了某個自定義特性的類。

static IEnumerable<Type> GetTypesWithAttribute(Assembly assembly)
{
  foreach(Type type inassembly.GetTypes())
  {
    if (type.GetCustomAttributes(typeof(MyAttribute), true).Length > 0)
    {
      yield return type;
    }
  }
}

示例四,在運行時讀取自定義特性的值

public static class AttributeExtensions
{
  public static TValue GetAttribute<TAttribute, TValue>(
    this Type type,
    string MemberName,
    Func<TAttribute, TValue> valueSelector,
    bool inherit = false)
    where TAttribute : Attribute
  {
    var att = type.GetMember(MemberName).FirstOrDefault()
      .GetCustomAttributes(typeof(TAttribute), inherit)
      .FirstOrDefault() as TAttribute;
    if (att != null)
    {
      return valueSelector(att);
    }
    return default;
  }
}

// 使用:

class Program
{
  static void Main()
  {
    // 讀取 MyClass 類的 MyMethod 方法的 Description 特性的值
    var description = typeof(MyClass)
      .GetAttribute("MyMethod", (DescriptionAttribute d) => d.Description);
    Console.WriteLine(description); // 輸出:Hello
  }
}
public class MyClass
{
  [Description("Hello")]
  public void MyMethod() { }
}

動態(tài)實例化接口的所有實現(xiàn)類(插件激活)

通過反射來動態(tài)實例化某個接口的所有實現(xiàn)類,常用于實現(xiàn)系統(tǒng)的插件式開發(fā)。比如在程序啟動的時候去讀取指定文件夾(如 Plugins)中的 dll 文件,通過反射獲取 dll 中所有實現(xiàn)了某個接口的類,并在適當?shù)臅r候?qū)⑵鋵嵗?。大致實現(xiàn)如下:

interface IPlugin
{
  string Description { get; }
  void DoWork();
}

某個在獨立 dll 中的類:

class HelloPlugin : IPlugin
{
  public string Description => "A plugin that says Hello";
  public void DoWork()
  {
    Console.WriteLine("Hello");
  }
}

在你的系統(tǒng)啟動的時候動態(tài)加載該 dll,讀取實現(xiàn)了 IPlugin 接口的所有類的信息,并將其實例化。

public IEnumerable<IPlugin> InstantiatePlugins(string directory)
{
  var assemblyNames = Directory.GetFiles(directory, "*.addin.dll")
    .Select(name => new FileInfo(name).FullName).ToArray();

  foreach (var fileName assemblyNames)
    AppDomain.CurrentDomain.Load(File.ReadAllBytes(fileName));

  var assemblies = assemblyNames.Select(System.Reflection.Assembly.LoadFile);
  var typesInAssembly = assemblies.SelectMany(asm =>asm.GetTypes());
  var pluginTypes = typesInAssembly.Where(type => typeof (IPlugin).IsAssignableFrom(type));

  return pluginTypes.Select(Activator.CreateInstance).Cast<IPlugin>();
}

檢查泛型實例的泛型參數(shù)

前文提到了構(gòu)造泛型和具象泛型,這里解釋一下。大多時候我們所說的泛型都是指構(gòu)造泛型,有時候也被稱為具象泛型。比如 List<int> 就是一個構(gòu)造泛型,因為它可以通過 new 來實例化。相應(yīng)的,List<> 泛型是非構(gòu)造泛型,有時候也被稱為開放泛型,它不能被實例化。開放泛型通過反射可以轉(zhuǎn)換為任意的具象泛型,這一點前文有示例。

假如現(xiàn)在有一個泛型實例,出于某種需求,我們想知道構(gòu)建這個泛型實例需要用什么泛型參數(shù)。比如某人創(chuàng)建了一個 List<T> 泛型的實例,并把它作為參數(shù)傳給了我們的一個方法:

var myList = newList<int>();
ShowGenericArguments(myList);

我們的方法簽名是這樣的:

public void ShowGenericArguments(object o)

這時,作為此方法的編寫者,我們并不知道這個 o 對象具體是用什么類型的泛型參數(shù)構(gòu)建的。通過反射,我們可以得到泛型實例的很多信息,其中最簡單的就是判斷一個類型是不是泛型:

public void ShowGenericArguments(object o)
{
  if (o == null) return;
  Type t =o.GetType();
  if (!t.IsGenericType) return;
  ...
}

由于 List<> 本身也是泛型,所以上面的判斷不嚴謹,我們需要知道的是對象是不是一個構(gòu)造泛型(List<int>)。而 Type 類還提供了一些有用的屬性:

typeof(List<>).IsGenericType // true
typeof(List<>).IsGenericTypeDefinition // true
typeof(List<>).IsConstructedGenericType// false

typeof(List<int>).IsGenericType // true
typeof(List<int>).IsGenericTypeDefinition // false
typeof(List<int>).IsConstructedGenericType// true

IsConstructedGenericType IsGenericTypeDefinition 分別用來判斷某個泛型是不是構(gòu)造泛型和非構(gòu)造泛型。

再結(jié)合 Type 的 GetGenericArguments() 方法,就可以很容易地知道某個泛型實例是用什么泛型參數(shù)構(gòu)建的了,例如:

static void ShowGenericArguments(object o)
{
  if (o == null) return;
  Type t = o.GetType();
  if (!t.IsConstructedGenericType) return;
  foreach (Type genericTypeArgument in t.GetGenericArguments())
    Console.WriteLine(genericTypeArgument.Name);
}

以上就是深入了解C#之反射的詳細內(nèi)容,更多關(guān)于c# 反射的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • unity繪制一條流動的弧線(貝塞爾線)

    unity繪制一條流動的弧線(貝塞爾線)

    這篇文章主要為大家詳細介紹了unity繪制一條流動弧線的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C#使用yield關(guān)鍵字構(gòu)建迭代器詳解

    C#使用yield關(guān)鍵字構(gòu)建迭代器詳解

    這篇文章主要為大家詳細介紹了C#使用yield關(guān)鍵字構(gòu)建迭代器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 基于WPF實現(xiàn)多選下拉控件的示例代碼

    基于WPF實現(xiàn)多選下拉控件的示例代碼

    這篇文章主要為大家詳細介紹了WPF實現(xiàn)簡單的多選下拉控件,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下
    2023-02-02
  • C#?Web實現(xiàn)文件上傳的示例詳解

    C#?Web實現(xiàn)文件上傳的示例詳解

    這篇文章主要為大家詳細介紹了C#?Web實現(xiàn)文件上傳的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • C#獲取web.config配置文件內(nèi)容的方法

    C#獲取web.config配置文件內(nèi)容的方法

    這篇文章主要介紹了C#獲取web.config配置文件內(nèi)容的方法,涉及C#配置文件屬性獲取的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C#實現(xiàn)讀取寫入Json文件

    C#實現(xiàn)讀取寫入Json文件

    這篇文章主要介紹了C#實現(xiàn)讀取寫入Json文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • c# 實現(xiàn)發(fā)送郵件的功能

    c# 實現(xiàn)發(fā)送郵件的功能

    這篇文章主要介紹了c# 如何實現(xiàn)發(fā)送郵件的功能,文中示例代碼非常詳細,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#?將?Stream?保存到文件的方法

    C#?將?Stream?保存到文件的方法

    這篇文章主要介紹了C#將?Stream保存到文件的方法,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • C#使用SQLDMO操作數(shù)據(jù)庫的方法

    C#使用SQLDMO操作數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#使用SQLDMO操作數(shù)據(jù)庫的方法,實例分析了基于SQLDMO.dll動態(tài)鏈接庫操作數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • c#中多線程間的同步示例詳解

    c#中多線程間的同步示例詳解

    使用線程時最頭痛的就是共享資源的同步問題,處理不好會得到錯誤的結(jié)果,所以下面這篇文章主要給大家介紹了關(guān)于c#中多線程間同步的相關(guān)資料,需要的朋友可以參考下
    2021-09-09

最新評論

内乡县| 墨江| 龙江县| 鹤庆县| 乡城县| 鹤庆县| 河南省| 微博| 彭州市| 松阳县| 星座| 双桥区| 福泉市| 嘉峪关市| 德庆县| 彭水| 介休市| 南投市| 怀仁县| 文安县| 子洲县| 贺兰县| 祁连县| 镇远县| 冕宁县| 平潭县| 梅河口市| 阜阳市| 台州市| 伊宁县| 咸丰县| 松江区| 合水县| 洛宁县| 察隅县| 杭锦后旗| 延安市| 浦城县| 霍州市| 海口市| 夏河县|