我已经测试了类、方法、字段、属性和枚举,以查看当这不是真的时候是否存在任何情况?
DotNetFiddle Example
using System;
public class Program
{
public static void Main()
{
var fooType = typeof(Foo);
ThrowIfNotEqual(fooType.Name, nameof(Foo));
var fi = fooType.GetField(nameof(Foo.field));
ThrowIfNotEqual(fi.Name, nameof(Foo.field));
var pi = fooType.GetProperty(nameof(Foo.property));
ThrowIfNotEqual(pi.Name, nameof(Foo.property));
var mi = fooType.GetMethod(nameof(Foo.method));
ThrowIfNotEqual(mi.Name, nameof(Foo.method));
var fi2 = fooType.GetNestedTypes()[0];
ThrowIfNotEqual(fi2.Name, nameof(Foo.myEnum));
ThrowIfNotEqual("TestThisMethod", "WorksAsExpected");
}
public static void ThrowIfNotEqual(string a, string b)
{
if (a != b) throw new InvalidOperationException($"Are Not Equal: {a} != {b}");
}
public class Foo
{
public string field;
public string property { get; set; }
public void method() { }
public enum myEnum
{
A
}
}
}
结果:
运行时异常(行-1):不相等:testThisMethod!=工作预期