当前位置: 首页>>代码示例>>C#>>正文


C# PropertyDefinition.GetNormalPropertyBackingField方法代码示例

本文整理汇总了C#中Mono.Cecil.PropertyDefinition.GetNormalPropertyBackingField方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyDefinition.GetNormalPropertyBackingField方法的具体用法?C# PropertyDefinition.GetNormalPropertyBackingField怎么用?C# PropertyDefinition.GetNormalPropertyBackingField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mono.Cecil.PropertyDefinition的用法示例。


在下文中一共展示了PropertyDefinition.GetNormalPropertyBackingField方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddNotifications

        private static void AddNotifications(PropertyDefinition property_definition, MethodDefinition notify_method, MethodDefinition collection_notification_method)
        {
            // Find backing field
            FieldDefinition backing_field = property_definition.GetNormalPropertyBackingField().Resolve();

            // Import CollectionChanged methods
            Type backing_field_type = backing_field.GetAssemblyQualifiedName();
            MethodReference collection_remove_handler = property_definition.Module.Import(backing_field_type.GetMethod("remove_CollectionChanged"));
            MethodReference collection_add_handler = property_definition.Module.Import(backing_field_type.GetMethod("add_CollectionChanged"));
            MethodReference event_constructor = property_definition.Module.Import(typeof(NotifyCollectionChangedEventHandler).GetConstructors()[0]);

            // Add notifications
            ILProcessor processor = property_definition.SetMethod.Body.GetILProcessor();
            var first = property_definition.SetMethod.Body.Instructions.First();
            var ret = property_definition.SetMethod.Body.Instructions.Last(i => i.OpCode == OpCodes.Ret);
            var notify_property_nop = processor.Create(OpCodes.Nop);

            // If (property != null)
            processor.InsertBefore(first, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(first, processor.Create(OpCodes.Ldfld, backing_field));
            processor.InsertBefore(first, processor.Create(OpCodes.Ldnull));
            processor.InsertBefore(first, processor.Create(OpCodes.Ceq));
            processor.InsertBefore(first, processor.Create(OpCodes.Brtrue_S, first));

            // CollectionChanged -= notify method
            processor.InsertBefore(first, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(first, processor.Create(OpCodes.Ldfld, backing_field));
            processor.InsertBefore(first, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(first, processor.Create(OpCodes.Ldftn, collection_notification_method));
            processor.InsertBefore(first, processor.Create(OpCodes.Newobj, event_constructor));
            processor.InsertBefore(first, processor.Create(OpCodes.Callvirt, collection_remove_handler));

            // Existing code is here

            // If (property != null)
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldfld, backing_field));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldnull));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ceq));
            processor.InsertBefore(ret, processor.Create(OpCodes.Brtrue_S, notify_property_nop));

            // CollectionChanged -= notify method
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldfld, backing_field));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldftn, collection_notification_method));
            processor.InsertBefore(ret, processor.Create(OpCodes.Newobj, event_constructor));
            processor.InsertBefore(ret, processor.Create(OpCodes.Callvirt, collection_add_handler));

            // NotifyPropertyChanged(property)
            processor.InsertBefore(ret, notify_property_nop);
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldstr, property_definition.Name));
            processor.InsertBefore(ret, processor.Create(OpCodes.Call, notify_method));

            // Return from method here
        }
开发者ID:lycilph,项目名称:Projects,代码行数:57,代码来源:AddNotificationsToNormalCollectionProperty.cs

示例2: AddConstructorInitialization

        private static void AddConstructorInitialization(PropertyDefinition property_definition)
        {
            // Find backing field
            FieldDefinition backing_field = property_definition.GetNormalPropertyBackingField().Resolve();

            // Fix constructor setup of property (ie. add event handler to property, do it by setting property to field)
            var constructor = property_definition.DeclaringType.GetConstructor();

            // Add initialization
            ILProcessor processor = constructor.Body.GetILProcessor();
            var ret = constructor.Body.Instructions.Last(i => i.OpCode == OpCodes.Ret);

            processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
            processor.InsertBefore(ret, processor.Create(OpCodes.Ldfld, backing_field));
            processor.InsertBefore(ret, processor.Create(OpCodes.Call, property_definition.SetMethod));
        }
开发者ID:lycilph,项目名称:Projects,代码行数:17,代码来源:AddNotificationsToNormalCollectionProperty.cs


注:本文中的Mono.Cecil.PropertyDefinition.GetNormalPropertyBackingField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。