Пример #1
0
 static partial void ValidateObjectExtension(ref ValidationErrorsBuilder e, PropertySignature p)
 {
     if (p.Getter is null && p.Setter is null)
     {
         e.AddErr("Getter or setter must specified", "getter");
         e.AddErr("Setter or getter must specified", "setter");
     }
     if (p.Getter is object)
     {
         if (!p.Getter.Params.IsEmpty)
         {
             e.AddErr("Getter must have no parameters", "getter", "params");
         }
     }
 }
Пример #2
0
        /// <summary> Creates a C# automatic property (i.e. `public bool X { get; }` or `public string Y { get; set; }`). Returns the property and its backing field. </summary>
        public static (FieldDef, PropertyDef) CreateAutoProperty(TypeSignature declType, string name, TypeReference propertyType, Accessibility accessibility = null, bool isReadOnly = true, bool isStatic = false, XmlComment doccomment = null)
        {
            accessibility = accessibility ?? Accessibility.APublic;

            var field    = new FieldSignature(declType, string.Format(AutoPropertyField, name), Accessibility.APrivate, propertyType, isStatic, isReadOnly);
            var fieldRef = field.SpecializeFromDeclaringType();
            var prop     = PropertySignature.Create(name, declType, propertyType, accessibility, isReadOnly ? null : accessibility, isStatic);

            var getter = MethodDef.CreateWithArray(prop.Getter, thisP => Expression.FieldAccess(fieldRef, thisP.SingleOrDefault()).Dereference());
            // getter.Attributes.Add(declaringType.Compilation.CompilerGeneratedAttribute());
            var setter = isReadOnly ? null :
                         MethodDef.CreateWithArray(prop.Setter, args => Expression.FieldAccess(fieldRef, args.Length == 1 ? null : args[0].Ref()).ReferenceAssign(args.Last()));

            return(new FieldDef(field),
                   new PropertyDef(prop, getter, setter).With(doccomment: doccomment));
        }
Пример #3
0
        /// <summary> Declares a property that overrides the <paramref name="overriddenProperty" /> in the specified declaring type. The property must be virtual or from an interface. </summary>
        public static PropertySignature Override(TypeSignature declaringType, PropertySignature overriddenProperty, OptParam <bool> isVirtual = default, bool isAbstract = false)
        {
            var isInterface = overriddenProperty.DeclaringType.Kind == "interface";

            if (!isInterface && !overriddenProperty.IsVirtual)
            {
                throw new ArgumentException($"Can't override non-virtual property {overriddenProperty}");
            }

            return(Create(overriddenProperty.Name,
                          declaringType,
                          overriddenProperty.Type,
                          overriddenProperty.Getter?.Accessibility,
                          overriddenProperty.Setter?.Accessibility,
                          isVirtual: isVirtual.ValueOrDefault(!isInterface && declaringType.CanOverride),
                          isOverride: !isInterface,
                          isAbstract: isAbstract));
        }
Пример #4
0
        /// <summary> Creates a PropertySignature of a property represented by System.Reflection type </summary>
        public static PropertySignature FromReflection(R.PropertyInfo prop)
        {
            prop = MethodSignature.SanitizeDeclaringTypeGenerics(prop);
            var declaringType = TypeSignature.FromType(prop.DeclaringType);
            var get           = prop.GetMethod?.Apply(MethodSignature.FromReflection);
            var set           = prop.SetMethod?.Apply(MethodSignature.FromReflection);
            var m             = get ?? set;

            var resultType = TypeReference.FromType(prop.PropertyType);

            return(PropertySignature.Create(prop.Name,
                                            declaringType,
                                            resultType,
                                            get?.Accessibility,
                                            set?.Accessibility,
                                            m.IsStatic,
                                            m.IsVirtual,
                                            m.IsOverride,
                                            m.IsAbstract));
        }
Пример #5
0
 /// <summary> Creates a static property definition with the specified getter and setter factories. In principle, this method is similar to <see cref="MethodDef.Create(MethodSignature, Func{ParameterExpression, Expression})" /> </summary>
 public static PropertyDef CreateStatic(
     PropertySignature signature,
     Expression getter,
     Func <ParameterExpression, Expression> setter = null)
 {
     _ = signature ?? throw new ArgumentNullException(nameof(signature));
     if (!signature.IsStatic)
     {
         throw new ArgumentException($"The property '{signature}' is not static, so PropertyDef.CreateStatic may not be used. Please use the Create method instead.", nameof(signature));
     }
     if (signature.Getter is object && getter is null)
     {
         throw new ArgumentNullException(nameof(getter), $"A getter body must be specified for property '{signature}'");
     }
     if (signature.Setter is object && setter is null)
     {
         throw new ArgumentNullException(nameof(getter), $"A setter body must be specified for property '{signature}'");
     }
     return(new PropertyDef(
                signature,
                getter == null || signature.Getter == null ? null : MethodDef.Create(signature.Getter, getter),
                setter == null || signature.Setter == null ? null : MethodDef.Create(signature.Setter, setter)
                ));
 }
Пример #6
0
 /// <summary> Creates an empty property definition. Useful when declaring an interface. </summary>
 public static PropertyDef InterfaceDef(PropertySignature signature, XmlComment doccomment = null) =>
 new PropertyDef(signature, signature.Getter?.Apply(MethodDef.InterfaceDef), signature.Setter?.Apply(MethodDef.InterfaceDef), ImmutableArray <PropertyReference> .Empty, doccomment);
Пример #7
0
 public PropertyDef(PropertySignature signature, MethodDef getter, MethodDef setter)
     : this(signature, getter, setter, ImmutableArray <PropertyReference> .Empty)
 {
 }