private void AddMaskingProperty(Cecil.TypeDefinition type, Cecil.PropertyDefinition property) { var maskedPropety = new Cecil.PropertyDefinition(property.Name + "Mask", Cecil.PropertyAttributes.None, ModuleDefinition.TypeSystem.String); //maskedPropety.GetMethod = type.Properties.Add(maskedPropety); }
private void InstantiateCollection(TypeDefinition typeDef, MethodDefinition[] constructors, PropertyDefinition propDef) { var constructor = constructors.First(); if (constructors.Length > 1) { constructor = constructors.SingleOrDefault(s => !s.HasParameters && !s.IsStatic); if (constructor == null) { this.Log.Error("Type " + typeDef.FullName + " does not have a parameterless constructor for instantiating collections in"); } } var insertIdx = constructor.Body.Instructions.Count - 1; constructor.Body.Instructions.Insert(insertIdx++, Instruction.Create(OpCodes.Nop)); constructor.Body.Instructions.Insert(insertIdx++, Instruction.Create(OpCodes.Ldarg_0)); constructor.Body.Instructions.Insert( insertIdx++, Instruction.Create( OpCodes.Newobj, MakeGeneric( typeDef.Module.Import( typeDef.Module.Import(typeof(List<>)) .MakeGenericInstanceType(propDef.PropertyType) .Resolve() .GetConstructors() .First(c => !c.HasParameters)), ((GenericInstanceType)propDef.PropertyType).GenericArguments.First()))); constructor.Body.Instructions.Insert(insertIdx, Instruction.Create(OpCodes.Call, propDef.SetMethod)); }
public static ImageSource GetIcon(PropertyDefinition property, bool isIndexer = false) { MemberIcon icon = isIndexer ? MemberIcon.Indexer : MemberIcon.Property; MethodAttributes attributesOfMostAccessibleMethod = GetAttributesOfMostAccessibleMethod(property); bool isStatic = (attributesOfMostAccessibleMethod & MethodAttributes.Static) != 0; return Images.GetIcon(icon, GetOverlayIcon(attributesOfMostAccessibleMethod), isStatic); }
public void PropertyDefinitions(AnalysisNet.Types.TypeDefinition analysisNetType, Cecil.TypeDefinition cecilTypeDef) { foreach (AnalysisNet.Types.PropertyDefinition analysisNetProp in analysisNetType.PropertyDefinitions) { Cecil.PropertyDefinition cecilProp = new Cecil.PropertyDefinition(analysisNetProp.Name, Cecil.PropertyAttributes.None, ReferenceGenerator.TypeReference(analysisNetProp.PropertyType)); if (analysisNetProp.Getter != null) { Cecil.MethodDefinition getterDef = ReferenceGenerator.MethodReference(analysisNetProp.Getter).Resolve(); cecilProp.GetMethod = getterDef; } if (analysisNetProp.Setter != null) { Cecil.MethodDefinition setterDef = ReferenceGenerator.MethodReference(analysisNetProp.Setter).Resolve(); cecilProp.SetMethod = setterDef; } SetCustomAttributes(analysisNetProp.Attributes, cecilProp.CustomAttributes); // Properties.Add sets this field //cecilProp.DeclaringType = ReferenceGenerator.TypeReference(analysisNetType).Resolve(); cecilTypeDef.Properties.Add(cecilProp); } }
static FieldDefinition TryGetField(TypeDefinition typeDefinition, PropertyDefinition property) { var propertyName = property.Name; var fieldsWithSameType = typeDefinition.Fields.Where(x => x.DeclaringType == typeDefinition).ToList(); foreach (var field in fieldsWithSameType) { //AutoProp if (field.Name == $"<{propertyName}>k__BackingField") { return field; } } foreach (var field in fieldsWithSameType) { //diffCase var upperPropertyName = propertyName.ToUpper(); var fieldUpper = field.Name.ToUpper(); if (fieldUpper == upperPropertyName) { return field; } //underScore if (fieldUpper == "_" + upperPropertyName) { return field; } } return GetSingleField(property); }
private static void AddCollectionCode(PropertyDefinition property, bool isFirst, Collection<Instruction> ins, VariableDefinition resultVariable, MethodDefinition method, TypeDefinition type) { if (isFirst) { ins.Add(Instruction.Create(OpCodes.Ldc_I4_0)); ins.Add(Instruction.Create(OpCodes.Stloc, resultVariable)); } ins.If( c => { LoadVariable(property, c, type); }, t => { LoadVariable(property, t, type); var enumeratorVariable = method.Body.Variables.Add(property.Name + "Enumarator", ReferenceFinder.IEnumerator.TypeReference); var currentVariable = method.Body.Variables.Add(property.Name + "Current", ReferenceFinder.Object.TypeReference); GetEnumerator(t, enumeratorVariable); AddCollectionLoop(resultVariable, t, enumeratorVariable, currentVariable); }, f => { }); }
internal static MonoPropertyInfo Create(IMonoStructType klass, int index, Cecil.PropertyDefinition pinfo) { TargetType type = klass.File.MonoLanguage.LookupMonoType(pinfo.PropertyType); bool is_static = false; MonoFunctionType getter, setter; TargetMemberAccessibility accessibility = TargetMemberAccessibility.Private; if (pinfo.SetMethod != null) { setter = klass.LookupFunction(pinfo.SetMethod); is_static = pinfo.SetMethod.IsStatic; accessibility = MonoMethodInfo.GetAccessibility(pinfo.SetMethod); } else { setter = null; } if (pinfo.GetMethod != null) { getter = klass.LookupFunction(pinfo.GetMethod); is_static = pinfo.GetMethod.IsStatic; accessibility = MonoMethodInfo.GetAccessibility(pinfo.GetMethod); } else { getter = null; } return(new MonoPropertyInfo( type, klass, index, is_static, pinfo, accessibility, getter, setter)); }
static FieldDefinition GetSingleField(PropertyDefinition property, Code code, MethodDefinition methodDefinition) { if (methodDefinition?.Body == null) { return null; } FieldReference fieldReference = null; foreach (var instruction in methodDefinition.Body.Instructions) { if (instruction.OpCode.Code == code) { //if fieldReference is not null then we are at the second one if (fieldReference != null) { return null; } var field = instruction.Operand as FieldReference; if (field != null) { if (field.DeclaringType != property.DeclaringType) { continue; } if (field.FieldType != property.PropertyType) { continue; } fieldReference = field; } } } return fieldReference?.Resolve(); }
public PropertyReferenceReflectionEmitter(MemberReferenceExpression memberReferenceExpression, Type target, MemberInfo member, ILGenerator ilGenerator, IOpCodeIndexer instructionsIndexer, IAstVisitor<ILGenerator, AstNode> visitor, List<LocalBuilder> locals, bool isSetter = false) : base(memberReferenceExpression, target, member, ilGenerator, instructionsIndexer, visitor, locals) { var propertyInfo = Member as PropertyInfo; _isSetter = isSetter; _propertyDefinition = MemberReference.Annotation<Cecil.PropertyDefinition>(); NonPublic = !_propertyDefinition.GetMethod.IsPublic; Type = _propertyDefinition.PropertyType.GetActualType(); if (isSetter) { _propertyMethod = propertyInfo.GetSetMethod(NonPublic); _emitPrivateAction = EmitPrivateStorePropertyReference; } else { _propertyMethod = propertyInfo.GetGetMethod(NonPublic); _emitPrivateAction = EmitPrivateLoadPropertyReference; } }
private void WeaveDependencyProperty(MethodBody staticCtorBody, FieldReference field, PropertyDefinition property) { var assembly = property.DeclaringType.Module.Assembly; var propertyType = assembly.ImportType(Type.GetType(property.PropertyType.FullName)); var getTypeFromHandle = assembly.ImportMethod(typeof(Type).GetMethod("GetTypeFromHandle")); var register = assembly.ImportMethod(typeof(DependencyProperty).GetMethod("Register", new[] { typeof(string), typeof(Type), typeof(Type) })); // ignore previously weaved DPs if (staticCtorBody.Instructions.Any(i => i.Operand != null && i.Operand.ToString() == field.ToString())) { return; } var ret = staticCtorBody.Instructions.Last(); if (ret.OpCode != OpCodes.Ret) throw new InvalidOperationException("The last instruction should be OpCode.Ret"); HasChanges = true; var proc = staticCtorBody.GetILProcessor(); proc.InsertBefore(ret, proc.Create(OpCodes.Ldstr, property.Name)); proc.InsertBefore(ret, proc.Create(OpCodes.Ldtoken, propertyType)); proc.InsertBefore(ret, proc.Create(OpCodes.Call, getTypeFromHandle)); proc.InsertBefore(ret, proc.Create(OpCodes.Ldtoken, property.DeclaringType)); proc.InsertBefore(ret, proc.Create(OpCodes.Call, getTypeFromHandle)); proc.InsertBefore(ret, proc.Create(OpCodes.Call, register)); proc.InsertBefore(ret, proc.Create(OpCodes.Stsfld, field)); }
public static MethodDefinition AddPropertyGetter( PropertyDefinition property , MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot | MethodAttributes.Virtual , FieldDefinition backingField = null) { if (backingField == null) { // TODO: Try and find existing friendly named backingFields first. backingField = AddPropertyBackingField(property); } var methodName = "get_" + property.Name; var getter = new MethodDefinition(methodName, methodAttributes, property.PropertyType) { IsGetter = true, Body = {InitLocals = true}, }; getter.Body.Variables.Add(new VariableDefinition(property.PropertyType)); var returnStart = Instruction.Create(OpCodes.Ldloc_0); getter.Body.Instructions.Append( Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldfld, backingField), Instruction.Create(OpCodes.Stloc_0), Instruction.Create(OpCodes.Br_S, returnStart), returnStart, Instruction.Create(OpCodes.Ret) ); property.GetMethod = getter; property.DeclaringType.Methods.Add(getter); return getter; }
public static MethodDefinition AddPropertySetter( PropertyDefinition property , MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot | MethodAttributes.Virtual , FieldDefinition backingField = null) { if (backingField == null) { // TODO: Try and find existing friendly named backingFields first. backingField = AddPropertyBackingField(property); } var methodName = "set_" + property.Name; var setter = new MethodDefinition(methodName, methodAttributes, property.Module.TypeSystem.Void) { IsSetter = true, Body = { InitLocals = true }, }; setter.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, property.PropertyType)); setter.Body.Instructions.Append( Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldarg_1), Instruction.Create(OpCodes.Stfld, backingField), Instruction.Create(OpCodes.Ret) ); property.SetMethod = setter; property.DeclaringType.Methods.Add(setter); return setter; }
public ChangeFieldReferencesToPropertyVisitor(FieldDefinition field, PropertyDefinition property) { if (field == null) throw new ArgumentNullException("field"); if (property == null) throw new ArgumentNullException("property"); _field = field; _property = property; }
public IEnumerable<string> GetAlreadyNotifies(PropertyDefinition propertyDefinition) { if (propertyDefinition.SetMethod.IsAbstract) { yield break; } var instructions = propertyDefinition.SetMethod.Body.Instructions; for (var index = 0; index < instructions.Count; index++) { var instruction = instructions[index]; foreach (var methodName in EventInvokerNames) { int propertyNameIndex; if (instruction.IsCallToMethod(methodName, out propertyNameIndex)) { var before = instructions[index - propertyNameIndex]; if (before.OpCode == OpCodes.Ldstr) { yield return (string) before.Operand; } } } } }
public PropertyKey( TypeKey typeKey, PropertyDefinition prop ) { this.typeKey = typeKey; this.type = prop.PropertyType.FullName; this.name = prop.Name; this.getterMethodAttributes = prop.GetMethod != null ? prop.GetMethod.Attributes : 0; }
protected FieldDefinition GetBackingField(PropertyDefinition propertyDef) { // have a look for a field matching the standard format var fieldDef = propertyDef.DeclaringType.Fields.SingleOrDefault(f => f.Name == string.Format(BackingFieldTemplate, propertyDef.Name)); if (fieldDef != null) { return fieldDef; } // look for stfld il in the setter var candidates = propertyDef.SetMethod.Body.Instructions.Where( i => i.OpCode == OpCodes.Stfld && i.Operand is FieldDefinition && ((FieldDefinition)i.Operand).FieldType.FullName == propertyDef.PropertyType.FullName).ToArray(); if (candidates.Length == 1) { // they only store one thing in a field return (FieldDefinition)candidates.First().Operand; } else if (candidates.Count(i => i.Previous != null && i.Previous.OpCode == OpCodes.Ldarg_1) == 1) { // they only store one thing in a field by the previous instruction is to load the "value" on to the stack return (FieldDefinition)candidates.Single(i => i.Previous != null && i.Previous.OpCode == OpCodes.Ldarg_1).Operand; } // look for fields of this type loaded on to the stack candidates = propertyDef.GetMethod.Body.Instructions.Where( i => i.OpCode == OpCodes.Ldfld && i.Operand is FieldDefinition && ((FieldDefinition)i.Operand).FieldType.FullName == propertyDef.PropertyType.FullName).ToArray(); if (candidates.Length == 1) { return (FieldDefinition)candidates.First().Operand; } this.Log.Error("Unable to determine backing field for property " + propertyDef.FullName); return null; }
public static MethodDefinition Execute(PropertyDefinition property_definition, MethodDefinition notify_method, DependencyMap map) { log.Trace("\t\t\t\t\tAdding collection notification method for " + property_definition.Name); // Create method TypeReference return_type = property_definition.Module.Import(typeof (void)); MethodDefinition collection_notification = new MethodDefinition(property_definition.Name + "CollectionNotification", Mono.Cecil.MethodAttributes.Private | Mono.Cecil.MethodAttributes.HideBySig, return_type); // Add parameters TypeReference sender_type = property_definition.Module.Import(typeof(object)); TypeReference args_type = property_definition.Module.Import(typeof(NotifyCollectionChangedEventArgs)); ParameterDefinition sender = new ParameterDefinition("sender", Mono.Cecil.ParameterAttributes.None, sender_type); ParameterDefinition args = new ParameterDefinition("args", Mono.Cecil.ParameterAttributes.None, args_type); collection_notification.Parameters.Add(sender); collection_notification.Parameters.Add(args); // Add notifications for dependent properties ILProcessor processor = collection_notification.Body.GetILProcessor(); foreach (var target in map.GetDependenciesFor(property_definition.Name)) { log.Trace("\t\t\t\t\t\tAdding dependency " + target); processor.Emit(OpCodes.Ldarg_0); processor.Emit(OpCodes.Ldstr, target); processor.Emit(OpCodes.Call, notify_method); } processor.Emit(OpCodes.Ret); // Add method to class property_definition.DeclaringType.Methods.Add(collection_notification); return collection_notification; }
public CecilPropertyDescriptor (CecilWidgetLibrary lib, XmlElement elem, Stetic.ItemGroup group, Stetic.ClassDescriptor klass, PropertyDefinition pinfo): base (elem, group, klass) { string tname; if (pinfo != null) { name = pinfo.Name; tname = pinfo.PropertyType.FullName; canWrite = pinfo.SetMethod != null; } else { name = elem.GetAttribute ("name"); tname = elem.GetAttribute ("type"); canWrite = elem.Attributes ["canWrite"] == null; } Load (elem); type = Stetic.Registry.GetType (tname, false); if (type == null) { Console.WriteLine ("Could not find type: " + tname); type = typeof(string); } if (type.IsValueType) initialValue = Activator.CreateInstance (type); // Consider all properties runtime-properties, since they have been created // from class properties. isRuntimeProperty = true; if (pinfo != null) SaveCecilXml (elem); }
public static bool AlreadyHasEquality(PropertyDefinition propertyDefinition, FieldReference backingFieldReference) { var instructions = propertyDefinition.SetMethod.Body.Instructions; var list = instructions.Where(IsNotNop).ToList(); if (list.Count < 4) { return false; } var firstFive = list.Take(5).ToList(); if (firstFive.All(x => x.OpCode != OpCodes.Ldarg_1)) { return false; } if (firstFive.All(x => x.OpCode != OpCodes.Ldarg_0)) { return false; } if (firstFive.All(x => !x.IsEquality())) { return false; } if (firstFive.Any(x => x.Operand == backingFieldReference)) { return true; } if (firstFive.Any(x => x.Operand == propertyDefinition)) { return true; } return false; }
public PropertyKey(TypeKey typeKey, PropertyDefinition prop) { this.typeKey = typeKey; this.type = prop.PropertyType.FullName; this.name = prop.Name; this.propertyDefinition = prop; }
public void Add(PropertyDefinition value) { if (!Contains (value)) Attach (value); List.Add (value); }
int AddBeforeAfterInvokerCall(int index, PropertyDefinition property) { var beforeVariable = new VariableDefinition(msCoreReferenceFinder.ObjectTypeReference); setMethodBody.Variables.Add(beforeVariable); var afterVariable = new VariableDefinition(msCoreReferenceFinder.ObjectTypeReference); setMethodBody.Variables.Add(afterVariable); var isVirtual = property.GetMethod.IsVirtual; var getMethod = property.GetMethod.GetGeneric(); index = instructions.Insert(index, Instruction.Create(OpCodes.Ldarg_0), CreateCall(getMethod), //TODO: look into why this box is required Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType), Instruction.Create(OpCodes.Stloc, afterVariable), Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldstr, property.Name), Instruction.Create(OpCodes.Ldloc, beforeVariable), Instruction.Create(OpCodes.Ldloc, afterVariable), CallEventInvoker() ); instructions.Prepend( Instruction.Create(OpCodes.Ldarg_0), CreateCall(getMethod), //TODO: look into why this box is required Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType), Instruction.Create(OpCodes.Stloc, beforeVariable)); return index + 4; }
public static void Execute(PropertyDefinition property_definition, MethodDefinition notify_method, DependencyMap map) { // Check if notifications are already call, if so bail out foreach (var instruction in property_definition.SetMethod.Body.Instructions) { if (instruction.OpCode == OpCodes.Call) { var method = instruction.Operand as MethodDefinition; if (method != null && method == notify_method) { log.Trace("\t\t\t\t\tBailing out, notification found in property"); return; } } } // Add notifications var ret = property_definition.SetMethod.Body.Instructions.Last(i => i.OpCode == OpCodes.Ret); ILProcessor processor = property_definition.SetMethod.Body.GetILProcessor(); // NotifyPropertyChanged(property) 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)); // Add notifications for dependent properties foreach (var target in map.GetDependenciesFor(property_definition.Name)) { log.Trace("\t\t\t\t\tAdding dependency " + target); processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0)); processor.InsertBefore(ret, processor.Create(OpCodes.Ldstr, target)); processor.InsertBefore(ret, processor.Create(OpCodes.Call, notify_method)); } }
public bool CheckIfGetterCallsVirtualBaseSetter(PropertyDefinition propertyDefinition) { if (propertyDefinition.SetMethod.IsVirtual) { var baseType = Resolve(propertyDefinition.DeclaringType.BaseType); var baseProperty = baseType.Properties.FirstOrDefault(x => x.Name == propertyDefinition.Name); if (baseProperty != null) { if (propertyDefinition.GetMethod != null) { var instructions = propertyDefinition.GetMethod.Body.Instructions; foreach (var instruction in instructions) { if (instruction.OpCode == OpCodes.Call && instruction.Operand is MethodReference && ((MethodReference) instruction.Operand).Resolve() == baseProperty.SetMethod) { return true; } } } } } return false; }
public MyPropertyInfo(PropertyDefinition propertyDefinition, MyClassInfo declaringType) : base() { this.name = propertyDefinition.Name; string[] readableForms = Tools.GetHumanReadableForms(propertyDefinition.PropertyType); this.typeFullName = readableForms[0]; this.typeFullNameWithoutRevArrayStrings = readableForms[1]; this.declaringType = declaringType; MethodDefinition getterInfo = propertyDefinition.GetMethod; MethodDefinition setterInfo = propertyDefinition.SetMethod; this.hasGetter = getterInfo != null; this.hasSetter = setterInfo != null; MethodDefinition getterOrSetterInfo = getterInfo != null ? getterInfo : setterInfo; Debug.Assert(getterOrSetterInfo != null, "Impossible! Property must have either getter or setter or both."); this.attributes = GetMyPropertyAttributes(propertyDefinition); this.underlyingMethodsAttributes = GetMyInvokableMemberAttributes(getterOrSetterInfo); this.parametersNames = new List<string>(); this.parameters = new Dictionary<string, MyParameterInfo>(); this.exceptionsDescrs = new List<ExceptionDescr>(); AddParameters(getterInfo, setterInfo); this.CheckSupport(propertyDefinition.Attributes, getterOrSetterInfo.Attributes); }
public AnalyzedPropertyOverridesTreeNode(PropertyDefinition analyzedProperty) { if (analyzedProperty == null) throw new ArgumentNullException("analyzedProperty"); this.analyzedProperty = analyzedProperty; }
int AddBeforeAfterInvokerCall(int index, PropertyDefinition property) { var beforeVariable = new VariableDefinition(typeSystem.Object); setMethodBody.Variables.Add(beforeVariable); var afterVariable = new VariableDefinition(typeSystem.Object); setMethodBody.Variables.Add(afterVariable); var getMethod = property.GetMethod.GetGeneric(); index = instructions.Insert(index, Instruction.Create(OpCodes.Ldarg_0), CreateCall(getMethod), Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType), Instruction.Create(OpCodes.Stloc, afterVariable), Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldstr, property.Name), Instruction.Create(OpCodes.Ldloc, beforeVariable), Instruction.Create(OpCodes.Ldloc, afterVariable), CallEventInvoker() ); instructions.Prepend( Instruction.Create(OpCodes.Ldarg_0), CreateCall(getMethod), Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType), Instruction.Create(OpCodes.Stloc, beforeVariable)); return index + 4; }
public PropertyReferenceReflectionEmitter(MemberReferenceExpression memberReferenceExpression, Type target, MemberInfo member, ILGenerator ilGenerator, IOpCodeIndexer instructionsIndexer, IAstVisitor <ILGenerator, AstNode> visitor, List <LocalBuilder> locals, bool isSetter = false) : base(memberReferenceExpression, target, member, ilGenerator, instructionsIndexer, visitor, locals) { var propertyInfo = Member as PropertyInfo; _isSetter = isSetter; _propertyDefinition = MemberReference.Annotation <Cecil.PropertyDefinition>(); NonPublic = !_propertyDefinition.GetMethod.IsPublic; Type = _propertyDefinition.PropertyType.GetActualType(); if (isSetter) { _propertyMethod = propertyInfo.GetSetMethod(NonPublic); _emitPrivateAction = EmitPrivateStorePropertyReference; } else { _propertyMethod = propertyInfo.GetGetMethod(NonPublic); _emitPrivateAction = EmitPrivateLoadPropertyReference; } }
private static string SignatureForProperty(PropertyDefinition property, bool hyperLinked) { var hb = new HtmlBuilder(); if (property.IsStatic()) { hb.AddKeyword("static"); hb.Add(" "); } hb.AddKeyword("public"); hb.Add(" "); hb.AddTypeReference(property.PropertyType, hyperLinked); hb.Add(" "); hb.AddMemberReferenceName(property, false); hb.Add(" {"); if (property.GetMethod != null) { hb.Add(" "); hb.AddKeyword("get"); hb.Add(";"); } if (property.SetMethod != null) { hb.Add(" "); hb.AddKeyword("set"); hb.Add(";"); } hb.Add(" }"); return hb.ToString(); }
public override void ProcessProperty (PropertyDefinition property) { foreach (var attribute in GetPreserveAttributes (property)) { MarkMethod (property.GetMethod, attribute); MarkMethod (property.SetMethod, attribute); } }
public static bool AlreadyContainsNotification(PropertyDefinition propertyDefinition, string methodName) { var instructions = propertyDefinition.SetMethod.Body.Instructions; return instructions.Any(x => x.OpCode.IsCall() && x.Operand is MethodReference && ((MethodReference) x.Operand).Name == methodName); }
public AnalyzedInterfacePropertyImplementedByTreeNode(PropertyDefinition analyzedProperty) { if (analyzedProperty == null) throw new ArgumentNullException("analyzedProperty"); this.analyzedProperty = analyzedProperty; this.analyzedMethod = this.analyzedProperty.GetMethod ?? this.analyzedProperty.SetMethod; }
public void Visit(PropertyDefinition property, VisitorContext context) { if (IsLazy(property) && property.GetMethod != null && !IsLazy(property.GetMethod)) { var instrumentor = new DoubleCheckedLockingWeaver(property.GetMethod, context); instrumentor.Instrument(); } }
private void WeaveMaskTo(Cecil.PropertyDefinition property) { var getBody = property.GetMethod.Body; getBody.InitLocals = true; getBody.Variables.Add(new VariableDefinition(ModuleDefinition.TypeSystem.String)); var ilProcessor = getBody.GetILProcessor(); var returnInstruction = getBody.Instructions.Where(i => i.OpCode == OpCodes.Ret).Single(); foreach (var instruction in GetMaskInstructions()) { ilProcessor.InsertBefore(returnInstruction, instruction); } }
private void WeaveDebugWriteLineTo(Cecil.PropertyDefinition property) { var getBody = property.GetMethod.Body; var ilProcessor = getBody.GetILProcessor(); var currentInstruction = Instruction.Create(OpCodes.Nop); ilProcessor.InsertBefore(getBody.Instructions.First(), currentInstruction); foreach (var instruction in GetWriteLineInstructions()) { ilProcessor.InsertAfter(currentInstruction, instruction); currentInstruction = instruction; } }
void Visit(Mono.Cecil.PropertyDefinition property) { if (property.GetMethod != null) { if (property.GetMethod.HasBody || !definitionsOnly) { Visit(property.GetMethod); } } if (property.SetMethod != null) { if (property.SetMethod.HasBody || !definitionsOnly) { Visit(property.SetMethod); } } }
private MonoPropertyInfo(TargetType type, IMonoStructType klass, int index, bool is_static, Cecil.PropertyDefinition pinfo, TargetMemberAccessibility accessibility, MonoFunctionType getter, MonoFunctionType setter) : base(type, pinfo.Name, index, is_static, accessibility, getter, setter) { this.Klass = klass; this.GetterType = getter; this.SetterType = setter; bool is_compiler_generated; DebuggerTypeProxyAttribute type_proxy; MonoSymbolFile.CheckCustomAttributes(pinfo, out browsable_state, out debugger_display, out type_proxy, out is_compiler_generated); }
void get_properties() { lock (this) { if (properties != null) { return; } properties = new MonoPropertyInfo [TypeDef.Properties.Count]; for (int i = 0; i < properties.Length; i++) { Cecil.PropertyDefinition prop = TypeDef.Properties [i]; Cecil.MethodDefinition m = prop.GetMethod; if (m == null) { m = prop.SetMethod; } properties [i] = MonoPropertyInfo.Create(this, i, prop); } } }
private void AddShouldSerializeMethod(Cecil.TypeDefinition type, Cecil.PropertyDefinition property) { var shouldSerializeMethod = new Cecil.MethodDefinition("ShouldSerialize" + property.Name, Cecil.MethodAttributes.Public, ModuleDefinition.TypeSystem.Boolean); var body = shouldSerializeMethod.Body; body.InitLocals = true; body.Variables.Add(new VariableDefinition(ModuleDefinition.TypeSystem.Boolean)); var instructions = body.Instructions; instructions.Add(Instruction.Create(OpCodes.Nop)); instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0)); instructions.Add(Instruction.Create(OpCodes.Stloc_0)); var ldLoc0 = Instruction.Create(OpCodes.Ldloc_0); instructions.Add(Instruction.Create(OpCodes.Br_S, ldLoc0)); instructions.Add(ldLoc0); instructions.Add(Instruction.Create(OpCodes.Ret)); type.Methods.Add(shouldSerializeMethod); }
public virtual void VisitPropertyDefinition(PropertyDefinition property) { }
public static string Disassemble(DomCecilMethod method, bool markup) { if (method.MethodDefinition.IsPInvokeImpl) { return(GettextCatalog.GetString("Method is P/Invoke")); } if (method.MethodDefinition.Body == null) { IType type = method.DeclaringType; return(type == null || type.ClassType == ClassType.Interface ? GettextCatalog.GetString("Interface method") : GettextCatalog.GetString("Abstract method")); } StringBuilder result = new StringBuilder(); foreach (Instruction instruction in method.MethodDefinition.Body.Instructions) { if (markup) { result.Append("<b>"); } result.Append(GetInstructionOffset(instruction)); result.Append(markup ? ":</b> " : ": "); result.Append(instruction.OpCode); if (markup) { result.Append("<i>"); } if (instruction.Operand != null) { result.Append(' '); if (instruction.Operand is string) { result.Append('"'); result.Append(AssemblyBrowserWidget.FormatText(instruction.Operand.ToString())); result.Append('"'); } else if (instruction.Operand is Mono.Cecil.Cil.Instruction) { result.Append(GetInstructionOffset((Mono.Cecil.Cil.Instruction)instruction.Operand)); } else if (instruction.Operand is Mono.Cecil.TypeDefinition) { AppendLink(result, new DomCecilType((Mono.Cecil.TypeDefinition)instruction.Operand).HelpUrl, instruction.Operand.ToString()); } else if (instruction.Operand is Mono.Cecil.MethodDefinition) { Mono.Cecil.MethodDefinition md = instruction.Operand as Mono.Cecil.MethodDefinition; AppendLink(result, new DomCecilMethod(md) { DeclaringType = new DomCecilType(md.DeclaringType) }.HelpUrl, instruction.Operand.ToString()); } else if (instruction.Operand is Mono.Cecil.FieldDefinition) { Mono.Cecil.FieldDefinition fd = instruction.Operand as Mono.Cecil.FieldDefinition; AppendLink(result, new DomCecilField(fd) { DeclaringType = new DomCecilType(fd.DeclaringType) }.HelpUrl, instruction.Operand.ToString()); } else if (instruction.Operand is Mono.Cecil.PropertyDefinition) { Mono.Cecil.PropertyDefinition pd = instruction.Operand as Mono.Cecil.PropertyDefinition; AppendLink(result, new DomCecilProperty(pd) { DeclaringType = new DomCecilType(pd.DeclaringType) }.HelpUrl, instruction.Operand.ToString()); } else if (instruction.Operand is Mono.Cecil.TypeReference) { AppendLink(result, "T:" + ((TypeReference)instruction.Operand).FullName, instruction.Operand.ToString()); } else if (instruction.Operand is Mono.Cecil.MethodReference) { Mono.Cecil.MethodReference mr = instruction.Operand as Mono.Cecil.MethodReference; StringBuilder id = new StringBuilder(mr.DeclaringType.ToString()); bool isConstructor = mr.Name == ".ctor"; if (!isConstructor) { id.Append("." + mr.Name); } id.Append("("); for (int i = 0; i < mr.Parameters.Count; i++) { if (i > 0) { id.Append(','); } id.Append(mr.Parameters[i].ParameterType.FullName); } id.Append(")"); AppendLink(result, (isConstructor ? "C:" : "M:") + id, id.ToString()); } else if (instruction.Operand is Mono.Cecil.FieldReference) { Mono.Cecil.FieldReference fr = instruction.Operand as Mono.Cecil.FieldReference; string id = fr.DeclaringType + "." + fr.Name; AppendLink(result, "F:" + id, id); } else if (instruction.Operand is Mono.Cecil.PropertyReference) { Mono.Cecil.PropertyReference pr = instruction.Operand as Mono.Cecil.PropertyReference; string id = pr.DeclaringType + "." + pr.Name; AppendLink(result, "P:" + id, id); } else { result.Append(AssemblyBrowserWidget.FormatText(instruction.Operand.ToString())); } } if (markup) { result.Append("</i>"); } result.AppendLine(); } result.AppendLine(); return(result.ToString()); }
static IEnumerable <XElement> GetMdocMembers(XElement mdoc, MemberInfo member, RegisterAttribute register, out PropertyInfo property) { MethodInfo method = member as MethodInfo; if (method != null && method.IsSpecialName && !method.IsConstructor) { // member is a get or set method for a property, and the property // won't have a [Register] attribute in the docs. property = method.DeclaringType.Properties // (DefaultBindingFlags) .Single(p => (p.GetMethod == method) || (p.SetMethod == method)); string name = property.Name; return(mdoc.XPathSelectElements("Members/Member") .Where(m => m.Attribute("MemberName").Value == name && m.Element("MemberType").Value == "Property")); } property = null; string attribute = string.IsNullOrEmpty(register.Signature) && string.IsNullOrEmpty(register.Connector) ? string.Format("Android.Runtime.Register(\"{0}\")", register.Name) : string.Format("Android.Runtime.Register(\"{0}\", \"{1}\", \"{2}\")", register.Name, register.Signature, register.Connector); return (from m in mdoc.XPathSelectElements("Members/Member") where m.Elements("Attributes") .Elements("Attribute") .Elements("AttributeName") // now n.Value may have ", ApiSince=xx" suffix, which requires partial matching... .Any(n => string.CompareOrdinal(n.Value, 0, attribute, 0, attribute.Length - 1) == 0) select m); }
void AddGetItem() { var method = new MethodDefinition(DataErrorInfoFinder.InterfaceRef.FullName + ".get_Item", MethodAttributes, TypeSystem.String) { IsGetter = true, IsPrivate = true, SemanticsAttributes = MethodSemanticsAttributes.Getter, }; method.Overrides.Add(DataErrorInfoFinder.GetItemMethod); method.Parameters.Add(new ParameterDefinition(TypeSystem.String)); method.Body.Instructions.Append( Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldfld, ValidationTemplateField), Instruction.Create(OpCodes.Ldarg_1), Instruction.Create(OpCodes.Callvirt, DataErrorInfoFinder.GetItemMethod), Instruction.Create(OpCodes.Ret)); var property = new PropertyDefinition(DataErrorInfoFinder.InterfaceRef.FullName + ".Item", PropertyAttributes.None, TypeSystem.String) { GetMethod = method, }; TypeDefinition.Methods.Add(method); TypeDefinition.Properties.Add(property); }