Esempio n. 1
0
        static CSProperty PublicGetPubPrivSetBacking(CSType type, string name, bool declareField, bool setIsPublic, string backingFieldName = null)
        {
            if (!declareField && backingFieldName == null)
            {
                throw new ArgumentException("declareField must be true if there is no supplied field name", nameof(declareField));
            }
            backingFieldName = backingFieldName ?? MassageName(Exceptions.ThrowOnNull(name, nameof(name)));


            CSIdentifier backingIdent = new CSIdentifier(backingFieldName);
            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (new ICodeElement [] { CSReturn.ReturnLine(backingIdent) }, false, true);
            LineCodeElementCollection <ICodeElement> setCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSAssignment.Assign(backingFieldName, new CSIdentifier("value"))
            }, false, true);
            CSProperty prop = new CSProperty(type, CSMethodKind.None, new CSIdentifier(name), CSVisibility.Public,
                                             new CSCodeBlock(getCode),
                                             (setIsPublic ? CSVisibility.Public : CSVisibility.Private), new CSCodeBlock(setCode));

            if (declareField)
            {
                prop.Insert(0, CSFieldDeclaration.FieldLine(type, backingFieldName));
            }
            return(prop);
        }
Esempio n. 2
0
        public static CSProperty PublicGetBacking(CSType type, CSIdentifier name, CSIdentifier backingFieldName,
                                                  bool includeBackingFieldDeclaration = false, CSMethodKind methodKind = CSMethodKind.None)
        {
            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSReturn.ReturnLine(Exceptions.ThrowOnNull(backingFieldName, nameof(backingFieldName)))
            }, false, true);
            CSProperty prop = new CSProperty(type, methodKind, Exceptions.ThrowOnNull(name, nameof(name)),
                                             CSVisibility.Public, new CSCodeBlock(getCode),
                                             CSVisibility.Public, null);

            if (includeBackingFieldDeclaration)
            {
                prop.Insert(0, CSFieldDeclaration.FieldLine(type, backingFieldName));
            }
            return(prop);
        }