/// <summary> /// Creates a new property in the target assembly, but doesn't set its accessors. /// </summary> /// <param name="targetType">Type of the target.</param> /// <param name="yourProperty">Your property.</param> /// <param name="newPropAttr">The new property attribute.</param> /// <exception cref="PatchDeclerationException">Thrown if this member collides with another member, and the error cannot be resolved.</exception> private NewMemberStatus CreateNewProperty(TypeDefinition targetType, PropertyDefinition yourProperty, NewMemberAttribute newPropAttr) { if (newPropAttr.IsImplicit) { Log_implicitly_creating_member("property", yourProperty); } else { Log_creating_member("property", yourProperty); } var maybeDuplicate = targetType.GetProperty(yourProperty.Name, yourProperty.Parameters.Select(x => x.ParameterType)); if (maybeDuplicate != null) { Log_duplicate_member("property", yourProperty, maybeDuplicate); if ((DebugOptions & DebugFlags.CreationOverwrites) != 0) { Log_overwriting(); return NewMemberStatus.Continue; } if (newPropAttr.IsImplicit) { return NewMemberStatus.InvalidItem; } throw Errors.Duplicate_member("property", yourProperty.FullName, maybeDuplicate.FullName); } var targetPropertyType = FixTypeReference(yourProperty.PropertyType); var targetProperty = new PropertyDefinition(yourProperty.Name, yourProperty.Attributes, targetPropertyType) { HasThis = yourProperty.HasThis, Constant = yourProperty.Constant, HasDefault = yourProperty.HasDefault }; targetType.Properties.Add(targetProperty); foreach (var param in yourProperty.Parameters) { targetProperty.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, FixTypeReference(param.ParameterType))); } return NewMemberStatus.Continue; }
/// <summary> /// Creates a new property in the target assembly, but doesn't set its accessors. /// </summary> /// <param name="targetType">Type of the target.</param> /// <param name="yourProperty">Your property.</param> /// <param name="newPropAttr">The new property attribute.</param> /// <exception cref="PatchDeclerationException">Thrown if this member collides with another member, and the error cannot be resolved.</exception> private PropertyDefinition CreateNewProperty(TypeDefinition targetType, PropertyDefinition yourProperty, NewMemberAttribute newPropAttr) { if (newPropAttr.IsImplicit) { Log_implicitly_creating_member("property", yourProperty); } else { Log_creating_member("property", yourProperty); } var newName = newPropAttr.NewMemberName ?? yourProperty.Name; var maybeDuplicate = targetType.GetProperty(newName, yourProperty.Parameters.Select(x => x.ParameterType)); if (maybeDuplicate != null) { Log_duplicate_member("property", yourProperty, maybeDuplicate); newName = GetNameAfterCollision(newName); var prevName = newName; Log_name_changed("property", yourProperty, prevName, newName); } var targetProperty = CopyProperty(yourProperty, newName); targetType.Properties.Add(targetProperty); return targetProperty; }
private void AutoModifyProperty(TypeDefinition targetType, MemberActionAttribute propActionAttr, PropertyDefinition yourProp) { Log_modifying_member("property", yourProp); var modifiesMemberAttr = propActionAttr as ModifiesMemberAttribute; var newMemberAttr = propActionAttr as NewMemberAttribute; string targetPropName; ModificationScope scope; if (modifiesMemberAttr != null) { targetPropName = modifiesMemberAttr.MemberName ?? yourProp.Name; scope = modifiesMemberAttr.Scope; } else if (newMemberAttr != null) { targetPropName = yourProp.Name; scope = ModificationScope.All; } else { throw Errors.Unknown_action_attribute(propActionAttr); } var targetProp = targetType.GetProperty(targetPropName, yourProp.Parameters.Select(x => x.ParameterType)); if (targetProp == null) { throw Errors.Missing_member("property", yourProp, targetPropName); } if ((scope & ModificationScope.CustomAttributes) != 0) { CopyCustomAttributes(targetProp, yourProp); for (int i = 0; i < yourProp.Parameters.Count; i++) { CopyCustomAttributes(targetProp.Parameters[i], yourProp.Parameters[i]); } } if ((scope & ModificationScope.Body) != 0) { targetProp.GetMethod = yourProp.GetMethod != null ? FixMethodReference(yourProp.GetMethod).Resolve() : null; targetProp.SetMethod = yourProp.SetMethod != null ? FixMethodReference(yourProp.SetMethod).Resolve() : null; targetProp.OtherMethods.Clear(); if (yourProp.HasOtherMethods) { //I have absolutely NO idea what this is used for foreach (var otherMethod in yourProp.OtherMethods) { targetProp.OtherMethods.Add(FixMethodReference(otherMethod).Resolve()); } } } }