0Pricing
C# Academy · 课时

Type、MethodInfo、激活与自定义特性

获取 Type 对象,定位 MethodInfo,调用方法,使用 Activator 创建实例,并定义和读取自定义特性。

Type、MethodInfo、激活与自定义特性 是 CoddyKit 上的免费 C# Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。

概览

目标:安全地使用反射。

  • 获取Type
  • 查找MethodInfo
  • 通过激活器创建对象
  • 定义并读取特性

获取 Type

可以通过类型运算符、instance 的 GetType(),或按名称调用 Type.GetType 来获取Type。

using System;

public class Sample { }

public class Program
{
  public static void Main(string[] args)
  {
    // 1) From a compile-time type
    Type t1 = typeof(Sample);

    // 2) From an instance
    Sample s = new Sample();
    Type t2 = s.GetType();

    // 3) From a full name (needs assembly-qualified name for external types)
    Type t3 = Type.GetType("System.String");

    Console.WriteLine(t1.Name + ", " + t2.FullName + ", " + (t3 == null ? "null" : t3.Name));
  }
}

MethodInfo.Invoke

使用GetMethod定位方法,并使用Invoke调用它。传入 instance 和参数数组。

using System;
using System.Reflection;

public class Greeter
{
  public string Hello(string name) { return "Hello, " + name; }
}

public class Program
{
  public static void Main(string[] args)
  {
    Greeter g = new Greeter();
    Type t = typeof(Greeter);

    MethodInfo mi = t.GetMethod("Hello"); // public instance method
    object result = mi.Invoke(g, new object[] { "Ada" }); // call dynamically

    Console.WriteLine(result); // Hello, Ada
  }
}

Activator 基础

Activator.CreateInstance(Type) 使用公共的无参数构造函数创建对象。

using System;

public class Person
{
  public string Name { get; set; }
  public Person() { Name = "Unknown"; } // parameterless ctor
}

public class Program
{
  public static void Main(string[] args)
  {
    Type tp = typeof(Person);
    object o = Activator.CreateInstance(tp); // uses public parameterless ctor
    Person p = (Person)o;
    p.Name = "Alan";
    Console.WriteLine("Created: " + p.Name);
  }
}

自定义特性

创建一个小型的 Attribute 类,并通过类型或方法上的 GetCustomAttributes 获取它。

using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class LabelAttribute : Attribute
{
  public string Text { get; private set; }
  public LabelAttribute(string text) { Text = text; }
}

[Label("Utility class")]
public class Utils
{
  [Label("Adds two ints")]
  public static int Add(int a, int b) { return a + b; }
}

public class Program
{
  public static void Main(string[] args)
  {
    Type t = typeof(Utils);

    // Read class attribute
    object[] classAttrs = t.GetCustomAttributes(typeof(LabelAttribute), false);
    if (classAttrs.Length > 0)
      Console.WriteLine("Class label: " + ((LabelAttribute)classAttrs[0]).Text);

    // Read method attribute
    MethodInfo mi = t.GetMethod("Add");
    object[] methodAttrs = mi.GetCustomAttributes(typeof(LabelAttribute), false);
    if (methodAttrs.Length > 0)
      Console.WriteLine("Method label: " + ((LabelAttribute)methodAttrs[0]).Text);
  }
}

提示与安全性

提示:

  • 检查 Type.GetType 返回的 null,并检查缺失的成员。
  • 尽可能优先使用 typeof(更快、更安全)。
  • 将反射的使用集中管理,并完善相关文档。
  • 对于非公共成员或静态成员的查找,请使用 BindingFlags。

激活基础

快速检查:哪个 API 可以从具有公共无参数构造函数的 Type 创建实例?

回顾

回顾:获取 Type,查找 MethodInfo,使用 Activator 创建对象,并通过 GetCustomAttributes 读取特性。

常见问题解答

「Type、MethodInfo、激活与自定义特性」课时是免费的吗?

是的 — 「Type、MethodInfo、激活与自定义特性」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。

「Type、MethodInfo、激活与自定义特性」这节课中我会学到什么?

获取 Type 对象,定位 MethodInfo,调用方法,使用 Activator 创建实例,并定义和读取自定义特性。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 C# Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。

「Type、MethodInfo、激活与自定义特性」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 C# Academy 课中编写并运行代码吗?

能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Type、MethodInfo、激活与自定义特性
  2. 源代码级信息(Caller 特性)
  3. 轻量级元编程场景
← 返回 C# Academy