Пример #1
0
        private static FieldInfo MakePropertyChangedEvent(TypeBuilder typeBuilder)
        {
            MethodInfo    method        = typeof(Delegate).GetMethod("Combine", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(Delegate), typeof(Delegate) }, null);
            MethodInfo    methodInfo    = typeof(Delegate).GetMethod("Remove", BindingFlags.Static | BindingFlags.Public);
            MethodInfo    methodInfo1   = ((IEnumerable <MethodInfo>) typeof(Interlocked).GetMethods(BindingFlags.Static | BindingFlags.Public)).First <MethodInfo>((MethodInfo m) => (!string.Equals(m.Name, "CompareExchange") ? false : m.IsGenericMethod)).MakeGenericMethod(new Type[] { typeof(PropertyChangedEventHandler) });
            FieldBuilder  fieldBuilder  = typeBuilder.DefineField("PropertyChanged", typeof(PropertyChangedEventHandler), FieldAttributes.Private);
            EventBuilder  eventBuilder  = typeBuilder.DefineEvent("PropertyChanged", EventAttributes.None, typeof(PropertyChangedEventHandler));
            MethodBuilder methodBuilder = typeBuilder.DefineMethod("add_PropertyChanged", MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.VtableLayoutMask | MethodAttributes.NewSlot | MethodAttributes.SpecialName, typeof(void), new Type[] { typeof(PropertyChangedEventHandler) });

            ProxyObservable.MakeEventMethod(methodBuilder, fieldBuilder, methodInfo1, method);
            eventBuilder.SetAddOnMethod(methodBuilder);
            MethodBuilder methodBuilder1 = typeBuilder.DefineMethod("remove_PropertyChanged", MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.VtableLayoutMask | MethodAttributes.NewSlot | MethodAttributes.SpecialName, typeof(void), new Type[] { typeof(PropertyChangedEventHandler) });

            ProxyObservable.MakeEventMethod(methodBuilder1, fieldBuilder, methodInfo1, methodInfo);
            eventBuilder.SetRemoveOnMethod(methodBuilder1);
            return(fieldBuilder);
        }
Пример #2
0
        private static Type MakeDynamicType(string dynamicTypeName, Type baseType)
        {
            TypeBuilder typeBuilder = ProxyObservable.DynamicModule.DefineType(dynamicTypeName, TypeAttributes.Public | TypeAttributes.Serializable, baseType, new Type[] { typeof(INotifyPropertyChanged) });
            FieldInfo   fieldInfo   = ProxyObservable.MakeConstructor(typeBuilder, baseType);
            MethodInfo  methodInfo  = ProxyObservable.MakeOnPropertyChanged(typeBuilder, ProxyObservable.MakePropertyChangedEvent(typeBuilder));

            ProxyObservable.MakeProperties(typeBuilder, fieldInfo, methodInfo, baseType);
            return(typeBuilder.CreateType());
        }