protected static IWeakEventRegistrationSignatures Construct <E>(E @delegate, Action <IWeakEventRegistrationSignatures> unregister) { Func <Delegate, Action <IWeakEventRegistrationSignatures>, IWeakEventRegistrationSignatures> cTor = null; var delegateObj = @delegate as Delegate; var lTypeInfo = new DelegateTypeInfo(delegateObj.Method.DeclaringType, typeof(E)); using (CTorDictLock.ReadScope()) { if (!CTorDict.TryGetValue(lTypeInfo, out cTor)) { cTor = null; } } if (cTor != null) { return(cTor(delegateObj, unregister)); } using (CTorDictLock.WriteScope()) { if (!CTorDict.TryGetValue(lTypeInfo, out cTor)) { var genericType = typeof(WeakEventRegistration <,>).CreateGenericType(delegateObj.Method.DeclaringType, typeof(E)); var weakHandler = genericType.CreateInstance(delegateObj, unregister, true).CastTo <IWeakEventRegistrationSignatures>(); return(weakHandler); } } return(cTor(delegateObj, unregister)); }
private DelegateTypeInfo BuildInfo(BuildMethod method, List <TypeSignature> genericArguments) { var typeInfo = new DelegateTypeInfo(); int argumentCount = method.Parameters.Count; if (method.HasThis) { argumentCount++; } var arguments = new TypeSignature[argumentCount]; var parameterFlags = new int[argumentCount]; if (argumentCount > 0) { int index = 0; if (method.HasThis) { arguments[index++] = new GenericParameterType(false, 0); genericArguments.Add(TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly)); } var parameters = method.Parameters; for (int i = 0; i < parameters.Count; i++) { var parameter = parameters[i]; arguments[index] = BuildType(parameter.Type, genericArguments); parameterFlags[index] = GetParameterFlags(parameter); index++; } } var returnType = BuildType(method.ReturnType.Type, genericArguments); typeInfo.InvokeCallSite = new CallSite( true, false, MethodCallingConvention.Default, returnType, arguments, -1, 0); typeInfo.InvokeParameterFlags = parameterFlags; typeInfo.GenericParameterCount = genericArguments.Count; return(typeInfo); }
internal static string CreateUniqueMethodName(IComponent component, string propName, System.Type delegateType) { IServiceProvider serviceProvider = component.Site; if (serviceProvider == null) { throw new ArgumentException("component"); } IDesignerHost host = serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost; if (host == null) { throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IDesignerHost).FullName })); } if (((ITypeProvider)serviceProvider.GetService(typeof(ITypeProvider))) == null) { throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(ITypeProvider).FullName })); } string name = null; IReferenceService service = serviceProvider.GetService(typeof(IReferenceService)) as IReferenceService; if (service != null) { name = service.GetName(component); } else { ISite site = component.Site; if (site != null) { name = site.Name; } } if (name == null) { name = component.GetType().Name; } name = (name.Replace('.', '_').Replace('/', '_') + "_" + propName).Replace('(', '_').Replace(')', '_').Replace(" ", ""); DelegateTypeInfo info = new DelegateTypeInfo(delegateType); Activity rootComponent = host.RootComponent as Activity; if (rootComponent == null) { Activity activity2 = component as Activity; throw new InvalidOperationException(SR.GetString("Error_CantCreateMethod", new object[] { (activity2 != null) ? activity2.QualifiedName : string.Empty })); } System.Type dataSourceClass = Helpers.GetDataSourceClass(rootComponent, serviceProvider); if (dataSourceClass == null) { throw new InvalidOperationException(SR.GetString("Error_CantCreateMethod", new object[] { rootComponent.QualifiedName })); } BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; MethodInfo[] methods = dataSourceClass.GetMethods(bindingAttr); ArrayList list = new ArrayList(); foreach (MethodInfo info2 in methods) { if (info2.GetParameters().Length != info.Parameters.Length) { continue; } bool flag = true; for (int i = 0; i < info.Parameters.Length; i++) { ParameterInfo info3 = info2.GetParameters()[i]; CodeParameterDeclarationExpression expression = info.Parameters[i]; FieldDirection direction = expression.Direction; if ((((direction == FieldDirection.In) && !info3.IsIn) || ((direction == FieldDirection.Out) && !info3.IsOut)) || (((direction == FieldDirection.Ref) && (!info3.IsIn || !info3.IsOut)) || !Helpers.TypesEqual(expression.Type, info3.ParameterType))) { flag = false; break; } } if (flag) { list.Add(info2.Name); } } int num2 = 0; bool flag2 = true; string strB = name; MemberInfo[] members = dataSourceClass.GetMembers(); while (flag2 && (num2 < 0x7fffffff)) { flag2 = false; foreach (string str3 in list) { if (string.Compare(str3, strB, StringComparison.OrdinalIgnoreCase) == 0) { flag2 = true; break; } } if (!flag2) { foreach (MemberInfo info4 in members) { if (!(info4 is MethodInfo) && (string.Compare(info4.Name, strB, StringComparison.OrdinalIgnoreCase) == 0)) { flag2 = true; break; } } } if (!flag2) { MethodInfo info5 = host.RootComponent.GetType().GetMethod(strB, bindingAttr, null, info.ParameterTypes, null); if ((info5 != null) && !info5.IsPrivate) { flag2 = true; } } if (flag2) { strB = name + "_" + ++num2.ToString(CultureInfo.InvariantCulture); } } return(strB); }
internal static string CreateUniqueMethodName(IComponent component, string propName, Type delegateType) { IServiceProvider serviceProvider = component.Site as IServiceProvider; if (serviceProvider == null) throw new ArgumentException("component"); IDesignerHost host = serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost; if (host == null) throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IDesignerHost).FullName)); ITypeProvider typeProvider = (ITypeProvider)serviceProvider.GetService(typeof(ITypeProvider)); if (typeProvider == null) throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(ITypeProvider).FullName)); string name = null; IReferenceService referenceService = serviceProvider.GetService(typeof(IReferenceService)) as IReferenceService; if (referenceService != null) { name = referenceService.GetName(component); } else { ISite site = ((IComponent)component).Site; if (site != null) name = site.Name; } if (name == null) name = component.GetType().Name; name = name.Replace('.', '_'); name = name.Replace('/', '_') + "_" + propName; name = name.Replace('(', '_'); name = name.Replace(')', '_'); name = name.Replace(" ", ""); DelegateTypeInfo dti = new DelegateTypeInfo(delegateType); Activity contextActivity = host.RootComponent as Activity; if (contextActivity == null) { Activity activity = component as Activity; throw new InvalidOperationException(SR.GetString(SR.Error_CantCreateMethod, activity != null ? activity.QualifiedName : string.Empty)); } Type type = Helpers.GetDataSourceClass(contextActivity, serviceProvider); if (type == null) throw new InvalidOperationException(SR.GetString(SR.Error_CantCreateMethod, contextActivity.QualifiedName)); BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; MethodInfo[] methods = type.GetMethods(bindingFlags); ArrayList compatibleMethods = new ArrayList(); foreach (MethodInfo method in methods) { if (method.GetParameters().Length == dti.Parameters.Length) { bool match = true; for (int i = 0; i < dti.Parameters.Length; i++) { ParameterInfo right = method.GetParameters()[i]; CodeParameterDeclarationExpression left = dti.Parameters[i]; FieldDirection direction = left.Direction; if ((direction == FieldDirection.In && !right.IsIn) || (direction == FieldDirection.Out && !right.IsOut) || (direction == FieldDirection.Ref && !(right.IsIn && right.IsOut)) || !Helpers.TypesEqual(left.Type, right.ParameterType)) { match = false; break; } } if (match) compatibleMethods.Add(method.Name); } } int tryCount = 0; bool duplicate = true; string uniqueName = name; MemberInfo[] members = type.GetMembers(); while (duplicate && tryCount < Int32.MaxValue) { duplicate = false; foreach (string existingMethod in compatibleMethods) { if (string.Compare(existingMethod, uniqueName, StringComparison.OrdinalIgnoreCase) == 0) { duplicate = true; break; } } if (!duplicate) { foreach (MemberInfo member in members) { if (!(member is MethodInfo) && string.Compare(member.Name, uniqueName, StringComparison.OrdinalIgnoreCase) == 0) { duplicate = true; break; } } } if (!duplicate) { MethodInfo mi = host.RootComponent.GetType().GetMethod(uniqueName, bindingFlags, null, dti.ParameterTypes, null); if (mi != null && !mi.IsPrivate) duplicate = true; } if (duplicate) uniqueName = name + "_" + (++tryCount).ToString(CultureInfo.InvariantCulture); } return uniqueName; }