Пример #1
0
 /// <summary>
 /// 获取对应类型的事件信息集合
 /// </summary>
 /// <param name="type"></param>
 /// <param name="bindingFlag"></param>
 public static EventInfo[] GainEvents(Type type,
                                      BindingFlags bindingFlag = BindingFlags.Default)
 {
     return(GainMembers(type, bindingFlag.ToBindingFlags(),
                        MemberCategories.EventInfo)?.Cast <EventInfo>()?.ToArray() ??
            EmptyArrays.EventInfo);
 }
Пример #2
0
 /// <summary>
 /// 获取对应类型的属性信息集合
 /// </summary>
 /// <param name="type"></param>
 /// <param name="bindingFlag"></param>
 /// <returns></returns>
 public static PropertyInfo[] GainProperties(Type type,
                                             BindingFlags bindingFlag = BindingFlags.Default)
 {
     return(GainMembers(type, bindingFlag.ToBindingFlags(),
                        MemberCategories.Property)?.Cast <PropertyInfo>()?.ToArray() ??
            EmptyArrays.PropertyInfo);
 }
Пример #3
0
 /// <summary>
 /// 获取对应类型的事件信息
 /// </summary>
 /// <param name="type"></param>
 /// <param name="bindingFlag"></param>
 /// <param name="eventName"></param>
 public static EventInfo GainEvent(Type type,
                                   string eventName,
                                   BindingFlags bindingFlag = BindingFlags.Default)
 {
     return((GainEvents(type, bindingFlag.ToBindingFlags())?.ToArray() ??
             EmptyArrays.EventInfo)?.FirstOrDefault(x =>
                                                    x?.Name == eventName) ?? default);
 }
Пример #4
0
 /// <summary>
 /// 获取对应类型的属性名称信息
 /// </summary>
 /// <param name="type"></param>
 /// <param name="propertyName"></param>
 /// <param name="bindingFlag"></param>
 /// <returns></returns>
 public static PropertyInfo GainProperty(Type type,
                                         string propertyName,
                                         BindingFlags bindingFlag = BindingFlags.Default)
 {
     return((GainProperties(type, bindingFlag.ToBindingFlags())?.ToArray() ??
             EmptyArrays.PropertyInfo)?.FirstOrDefault(x =>
                                                       x?.Name == propertyName) ?? default);
 }
Пример #5
0
 /// <summary>
 /// 尝试添加对象委托事件
 /// 目标方法名称目标方法参数需符合事件参数否则会创建失败
 /// 参数 1 创建对象委托事件的对象或静态事件所属对象类型
 /// 参数 2 创建对象委托事件名称
 /// 参数 3 目标方法名称目标方法参数需符合事件参数
 /// 参数 4 委托事件所在载体或静态委托事件所在对象类型  为null时默认会将instance作为method载体
 /// 参数 5 创建对象委托事件的要插入的索引 如果为-1表示默认在最后插入 如果index超出list范围也会在最后插入
 /// 参数 6 创建对象委托事件的公开程度
 /// </summary>
 /// <param name="instance">创建对象委托事件的对象或静态事件所属对象类型</param>
 /// <param name="eventName">创建对象委托事件名称</param>
 /// <param name="methodName">目标方法名称目标方法参数需符合事件参数</param>
 /// <param name="methodCarrier">委托事件所在载体或静态委托事件所在对象类型  为null时默认会将instance作为method载体</param>
 /// <param name="index"> 创建对象委托事件的要插入的索引 如果为-1表示默认在最后插入 如果index超出list范围也会在最后插入</param>
 /// <param name="bindingFlag">创建对象委托事件的公开程度</param>
 /// <returns>创建的委托</returns>
 public static Delegate AttachEvent(object instance,
                                    string eventName,
                                    string methodName,
                                    object methodCarrier     = null,
                                    int index                = -1,
                                    BindingFlags bindingFlag = BindingFlags.Default)
 {
     try
     {
         methodCarrier = methodCarrier == default ? instance : methodCarrier;
         if ((instance is Type ? instance : instance?.GetType()) is Type instanceType &&
             GainEvent(instanceType, eventName, bindingFlag.ToBindingFlags())
             is EventInfo eventInfo)
         {
             if (GetFieldInfo(instanceType, eventInfo.Name) is FieldInfo eventFieldInfo)
             {
                 if (GetMethodInfo(methodCarrier is Type ?
                                   methodCarrier as Type : methodCarrier?.GetType(), methodName)
                     is MethodInfo methodInfo &&
                     AddOrGetEventParameters(eventInfo).ToArrayType() is Type[] eventParametersTypes &&
                     methodInfo.GetParameters().ToArrayType() is Type[] methodParametersTypes &&
                     eventParametersTypes.Equal(methodParametersTypes))
                 {
                     if ((methodInfo.IsStatic ?
                          Delegate.CreateDelegate(eventInfo.EventHandlerType,
                                                  methodCarrier as Type ?? methodCarrier?.GetType() ?? Types.@object,
                                                  methodName) :
                          Delegate.CreateDelegate(eventInfo.EventHandlerType,
                                                  methodCarrier,
                                                  methodName)) is Delegate del)
                     {
                         var eventdel = AddOrGetEventsDefaultDelegate(eventFieldInfo, instance);
                         var delArray = eventdel?.GetInvocationList() ?? EmptyArrays.Delegate;
                         if (index < 0 || index >= delArray.Length)
                         {
                             eventdel = eventdel != default ? Delegate.Combine(eventdel, del) : del;
                         }
                         else
                         {
                             var delList = delArray.ToList();
                             delList.Insert(index, del);
                             delArray = delList.ToArray();
                             eventdel = Delegate.Combine(delArray);
                         }
                         eventFieldInfo.SetValue(eventFieldInfo.IsStatic ? null : instance,
                                                 AddOrUpdateEventsDefaultDelegate(eventFieldInfo, eventdel));
                         return(eventdel);
                     }
                 }
             }
         }
     }
     catch (Exception exception)
     {
         throw exception;
     }
     return(default);
Пример #6
0
 /// <summary>
 /// 获取对应实例的属性值
 /// </summary>
 /// <param name="instance"></param>
 /// <param name="bindingFlag"></param>
 /// <param name="propertyName"></param>
 /// <param name="bindingFlag"></param>
 /// <returns></returns>
 public static object GetProperty(object instance,
                                  string propertyName,
                                  BindingFlags bindingFlag = BindingFlags.Default)
 {
     if (GainProperty(instance?.GetType(), propertyName, bindingFlag.ToBindingFlags())
         is PropertyInfo propertyInfo)
     {
         return(propertyInfo.GetValue(IsStatic(propertyInfo) ? null : instance));
     }
     return(default);
Пример #7
0
 /// <summary>
 /// 获取对应类型的事件信息
 /// </summary>
 /// <param name="type"></param>
 /// <param name="bindingFlag"></param>
 /// <param name="propertyName"></param>
 public static EventInfo GainEvent <T>(Expression <Func <T, object> > expression,
                                       BindingFlags bindingFlag = BindingFlags.Default)
 {
     return(GainEvent(typeof(T), expression.ToMemberName(), bindingFlag.ToBindingFlags()));
 }
Пример #8
0
 /// <summary>
 /// 获取对应类型的成员集合
 /// </summary>
 /// <param name="type"></param>
 /// <param name="bindingFlag"></param>
 /// <returns></returns>
 public static MemberInfo[] GainMembers(Type type,
                                        BindingFlags bindingFlag = BindingFlags.Default)
 {
     return(GainMembers(type, bindingFlag.ToBindingFlags(), MemberCategories.Member));
 }