我的主要问题是: 反射中是否可以区分某个委托类型的字段与事件用作存储字段的字段? 这就是问题:FieldInfo类是否包含有关它是否属于事件的信息,作为存储区?我无法找到任何可能显示的属性,也无法定位属性。反思:在运行时区分委托类型字段的事件字段
在下面的代码中,SomeField和SomeEvent的两个FieldInfos的相关属性都是相同的。所以我不知道如何根据它们是否是eventstoragefields来对FieldInfos进行排序。
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Test
{
class Program
{
public Action SomeField;
public event Action SomeEvent;
static void Main(string[] args)
{
FieldInfo[] fields = typeof(Program).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo fi in fields)
Console.WriteLine(string.Format("FieldName: {0}, Accessibility: {1}, Has Attributes: {2}.", fi.Name, fi.Attributes,
fi.GetCustomAttributes(true).Length != 0));
Console.ReadLine();
}
}
}
一种解决办法是寻找一个eventInfo具有完全相同的名字,但我不知道这是否是万无一失坦率地说,我不会满足于这种解决办法。必须有更直接的方式。
当然,必须有一种方式..... – JBSnorro 2010-08-31 15:08:58