protected override void GenerateCodePropertyField(MemberFieldDefinition field)
        {
            //TODO comments for fields
            //AddComments(field);
            string ptype = GetCLRTypeName(field);

            _codeBuilder.AppendFormatIndent("property {0} {1}\n{{\n", ptype, CodeStyleDefinition.ToCamelCase(field.NativeName));
            _codeBuilder.IncreaseIndent();
            _codeBuilder.AppendLine(ptype + " get()\n{");
            _codeBuilder.AppendLine("\treturn " + NameToPrivate(field) + ";");
            _codeBuilder.AppendLine("}");
            _codeBuilder.DecreaseIndent();
            _codeBuilder.AppendLine("}");
        }
Exemplo n.º 2
0
        public MetaDefinition(string file, string nativeNamespace, string managedNamespace, MetaConstructFactory factory, CodeStyleDefinition codeStyleDef)
        {
            _doc.Load(file);
            NativeNamespace  = nativeNamespace;
            ManagedNamespace = managedNamespace;
            Factory          = factory;
            CodeStyleDef     = codeStyleDef;

            // Find the root tag - assumes we only have one meta tag.
            XmlElement root = (XmlElement)_doc.GetElementsByTagName(META_TAG)[0];

            // Iterate through all namespaces
            foreach (XmlNode nsNode in root.ChildNodes)
            {
                XmlElement nsElement = nsNode as XmlElement;
                if (nsElement == null || nsElement.LocalName != NAMESPACE_TAG)
                {
                    // Not an XML element or not an namespace element, but something else.
                    continue;
                }

                AddNamespace(nsElement);
            }
        }
 public SourceCodeStringBuilder(CodeStyleDefinition codeStyleDef)
 {
     _codeStyleDef = codeStyleDef;
 }
        /// <summary>
        /// Converts the specified methods into CLR properties.
        /// </summary>
        /// <param name="methods">The methods to convert. Methods that are no accesors (<c>IsProperty == false</c>) will be ignored.</param>
        public static MemberPropertyDefinition[] GetPropertiesFromMethods(IEnumerable <MemberMethodDefinition> methods)
        {
            // NOTE: Use a sorted "list" here so that the order (and thereby the position) of thy properties in
            //   the generated source code doesn't vary each time the sources are generated.
            SortedDictionary <string, MemberPropertyDefinition> props = new SortedDictionary <string, MemberPropertyDefinition>();

            foreach (MemberMethodDefinition f in methods)
            {
                if (!f.IsProperty || !f.IsDeclarableFunction)
                {
                    continue;
                }

                MemberPropertyDefinition p    = null;
                string propName               = GetPropertyName(f);
                CodeStyleDefinition codeStyle = f.MetaDef.CodeStyleDef;

                if (props.ContainsKey(propName))
                {
                    p = props[propName];
                }
                else
                {
                    //
                    // Merge properties with "is" or "has" prefix as this prefix can only be determined
                    // from the get accessor but not from the set accessor.
                    //
                    if (f.IsPropertySetAccessor)
                    {
                        // Set accessor - check for existing properties prefixed with "is" or "has".
                        if (codeStyle.AllowIsInPropertyName && props.ContainsKey(codeStyle.CLRPropertyIsPrefix + propName))
                        {
                            propName = codeStyle.CLRPropertyIsPrefix + propName;
                            p        = props[propName];
                        }
                        else if (props.ContainsKey(codeStyle.CLRPropertyHasPrefix + propName))
                        {
                            propName = codeStyle.CLRPropertyHasPrefix + propName;
                            p        = props[propName];
                        }
                    }
                    else
                    {
                        // Get accessor
                        string oldPropName = null;
                        if (codeStyle.AllowIsInPropertyName &&
                            propName.StartsWith(codeStyle.CLRPropertyIsPrefix) &&
                            props.ContainsKey(propName.Substring(codeStyle.CLRPropertyIsPrefix.Length)))
                        {
                            // is prefix
                            oldPropName = propName.Substring(codeStyle.CLRPropertyIsPrefix.Length);
                        }
                        else if (propName.StartsWith(codeStyle.CLRPropertyHasPrefix) &&
                                 props.ContainsKey(propName.Substring(codeStyle.CLRPropertyHasPrefix.Length)))
                        {
                            // has prefix
                            oldPropName = propName.Substring(codeStyle.CLRPropertyHasPrefix.Length);
                        }

                        if (oldPropName != null)
                        {
                            MemberPropertyDefinition oldProp = props[oldPropName];
                            // We need to check here whether the set accessor is actually
                            if (oldProp.MemberTypeName == f.MemberTypeName)
                            {
                                props.Remove(oldPropName);

                                p = new MemberPropertyDefinition(propName);
                                p.MemberTypeName = oldProp.MemberTypeName;
                                p.PassedByType   = oldProp.PassedByType;
                                p.SetterFunction = oldProp.SetterFunction;

                                props.Add(p.Name, p);
                            }
                        }
                    }

                    if (p == null)
                    {
                        // New property found
                        p = new MemberPropertyDefinition(propName);
                        if (f.IsPropertyGetAccessor)
                        {
                            p.MemberTypeName = f.MemberTypeName;
                            p.PassedByType   = f.PassedByType;
                        }
                        else
                        {
                            p.MemberTypeName = f.Parameters[0].TypeName;
                            p.PassedByType   = f.Parameters[0].PassedByType;
                        }

                        props.Add(p.Name, p);
                    }
                }

                if (f.IsPropertyGetAccessor)
                {
                    p.GetterFunction = f;
                }
                else if (f.IsPropertySetAccessor)
                {
                    p.SetterFunction = f;
                }
            }

            return(props.Values.ToArray());
        }