示例#1
0
        public PropertyDeclaration AddProperty(
            FieldDeclaration f,
            string name,
            bool hasGet,
            bool hasSet,
            bool checkNonNull
            )
        {
            PropertyDeclaration p = this.AddProperty(
                f.Type,
                name);

            if (hasGet)
            {
                p.Get.Return(Expr.This.Field(f));
            }
            if (hasSet)
            {
                if (checkNonNull)
                {
                    ConditionStatement ifnull = Stm.If(Expr.Value.Identity(Expr.Null));
                    p.Set.Add(ifnull);
                    ifnull.TrueStatements.Add(
                        Stm.Throw(typeof(ArgumentNullException))
                        );
                    p.SetExceptions.Add(new ThrowedExceptionDeclaration(
                                            typeof(ArgumentNullException),
                                            "value is a null reference"
                                            ));
                }
                p.Set.Add(
                    Stm.Assign(
                        Expr.This.Field(f),
                        Expr.Value
                        )
                    );
            }

            return(p);
        }
        public PropertyDeclaration AddProperty(ITypeDeclaration type, string name)
        {
            if (type==null)
                throw new ArgumentNullException("type");
            if (name==null)
                throw new ArgumentNullException("name");

            PropertyDeclaration p = new PropertyDeclaration(this.Conformer.ToCapitalized(name),this,type);
            this.properties.Add(p);
            return p;
        }
        private void AttachXmlElementAttributes(PropertyDeclaration p, FieldInfo f)
        {
            string customName = null;
            // if array is type add element
            if (f.FieldType.IsArray && f.FieldType.GetElementType()!=typeof(Object))
            {
                AttributeDeclaration attr = p.CustomAttributes.Add(typeof(XmlElementAttribute));
                attr.Arguments.Add("ElementName",Expr.Prim(f.Name));
                //MMI:attr.Arguments.Add("Type",Expr.Prim(MapType(f.FieldType.GetElementType()).Name));
                attr.Arguments.Add("Type",Expr.TypeOf(MapType(f.FieldType.GetElementType())));
                customName = f.Name;
            }

            // attach xml elements
            foreach(XmlElementAttribute el in f.GetCustomAttributes(typeof(XmlElementAttribute),true))
            {
                if (customName == el.ElementName)
                    continue;
                AttributeDeclaration attr = p.CustomAttributes.Add(typeof(XmlElementAttribute));
                attr.Arguments.Add("ElementName",Expr.Prim(el.ElementName));
                if (el.Type!=null)
                {
                    //MMI:attr.Arguments.Add("Type",Expr.Prim(MapType(el.Type).Name));
                    attr.Arguments.Add("Type",Expr.TypeOf(MapType(el.Type)));
                }
            }
        }