示例#1
0
        CodeTypeDeclaration CreateMySettingsProperty(SettingsDocument setDoc)
        {
            CodeTypeDeclaration c = new CodeTypeDeclaration("MySettingsProperty");

            c.UserData["Module"] = true;
            c.AddAttribute(new CodeTypeReference("Microsoft.VisualBasic.HideModuleNameAttribute"));
            c.AddAttribute(typeof(DebuggerNonUserCodeAttribute));
            c.AddAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));
            c.TypeAttributes = TypeAttributes.NotPublic;

            CodeTypeReference r = new CodeTypeReference(setDoc.GeneratedFullClassName);
            var p = c.AddProperty(r, "Settings");

            p.Attributes = MemberAttributes.Assembly | MemberAttributes.Static;
            p.Getter.Return(Easy.Type(r).Property("Default"));
            p.AddAttribute(typeof(System.ComponentModel.Design.HelpKeywordAttribute), Easy.Prim("My.Settings"));
            return(c);
        }
示例#2
0
 /// <summary>
 /// Initializes the created enumeration
 /// </summary>
 /// <param name="input">The input NMeta enumeration</param>
 /// <param name="output">The generated code declaration</param>
 /// <param name="context">The transformation context</param>
 public override void Transform(IEnumeration input, CodeTypeDeclaration output, ITransformationContext context)
 {
     SetTypeReferenceForMappedType(input, CodeDomHelper.GetReferenceForType(output));
     base.Transform(input, output, context);
     if (input.AbsoluteUri != null)
     {
         output.AddAttribute(typeof(ModelRepresentationClassAttribute), input.AbsoluteUri.AbsoluteUri);
     }
     output.WriteDocumentation(input.Summary, input.Remarks);
 }
示例#3
0
        /// <summary>创建实体类</summary>
        public void Create()
        {
            Class                = new CodeTypeDeclaration(Name);
            Class.IsClass        = true;
            Class.IsPartial      = true;
            Class.TypeAttributes = TypeAttributes.Public;
            Class.AddSummary(Table.Description);

            // 特性
            Class.AddAttribute <SerializableAttribute>();
            Class.AddAttribute <DataObjectAttribute>();
            if (!Table.Description.IsNullOrWhiteSpace())
            {
                Class.AddAttribute <DescriptionAttribute>(Table.Description);
            }
            if (!Table.DisplayName.IsNullOrWhiteSpace() && Table.DisplayName != Table.Name)
            {
                Class.AddAttribute <DisplayNameAttribute>(Table.DisplayName);
            }
            Class.AddAttribute <CompilerGeneratedAttribute>();

            // 索引和关系
            if (Table.Indexes != null && Table.Indexes.Count > 0)
            {
                foreach (var item in Table.Indexes)
                {
                    if (item.Columns.Length < 1)
                    {
                        continue;
                    }

                    Class.AddAttribute <BindIndexAttribute>(item.Name, item.Unique, String.Join(",", item.Columns));
                }
            }
            if (Table.Relations != null && Table.Relations.Count > 0)
            {
                foreach (var item in Table.Relations)
                {
                    Class.AddAttribute <BindRelationAttribute>(item.Column, item.Unique, item.RelationTable, item.RelationColumn);
                }
            }

            // 绑定表
            Class.AddAttribute <BindTableAttribute>(Table.TableName, Table.Description, Table.ConnName ?? ConnName, Table.DbType, Table.IsView);

            // 基类
            Class.BaseTypes.Add(BaseType);
        }
示例#4
0
        public virtual CodeTypeDeclaration CreateClass(SettingsDocument setDoc)
        {
            CodeTypeDeclaration c = new CodeTypeDeclaration(setDoc.GeneratedClassName);

            c.AddAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));
            c.AddAttribute(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute),
                           Easy.Prim(typeof(SettingsCodeGeneratorTool).FullName),
                           Easy.Prim(typeof(SettingsCodeGeneratorTool).Assembly.GetName().Version.ToString()));
            c.TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed;
            c.IsPartial      = true;
            c.BaseTypes.Add(Easy.TypeRef(typeof(ApplicationSettingsBase)));

            CodeMemberField f = c.AddField(Easy.TypeRef(c), "defaultInstance");

            f.Attributes     = MemberAttributes.Private | MemberAttributes.Static;
            f.InitExpression = Easy.Type(typeof(ApplicationSettingsBase))
                               .InvokeMethod("Synchronized", Easy.New(Easy.TypeRef(c)))
                               .CastTo(Easy.TypeRef(c));

            var defaultProperty = c.AddProperty(f, "Default");

            if (setDoc.UseMySettingsClassName)
            {
                c.AddAttribute(typeof(EditorBrowsableAttribute), Easy.Prim(EditorBrowsableState.Advanced));
                AddAutoSaveLogic(c, defaultProperty);
            }

            foreach (SettingsEntry entry in setDoc.Entries)
            {
                Type           entryType      = entry.Type ?? typeof(string);
                SpecialSetting?specialSetting = null;
                foreach (SpecialTypeDescriptor desc in SpecialTypeDescriptor.Descriptors)
                {
                    if (desc.type == entryType)
                    {
                        entryType      = typeof(string);
                        specialSetting = desc.specialSetting;
                        break;
                    }
                }
                EasyProperty p = c.AddProperty(entryType, entry.Name);
                if (entry.Scope == SettingScope.User)
                {
                    p.AddAttribute(typeof(UserScopedSettingAttribute));
                }
                else
                {
                    p.AddAttribute(typeof(ApplicationScopedSettingAttribute));
                }
                if (!string.IsNullOrEmpty(entry.Provider))
                {
                    p.AddAttribute(typeof(SettingsProviderAttribute),
                                   Easy.TypeOf(new CodeTypeReference(entry.Provider)));
                }
                if (!string.IsNullOrEmpty(entry.Description))
                {
                    p.AddAttribute(typeof(SettingsDescriptionAttribute), Easy.Prim(entry.Description));
                    Easy.AddSummary(p, entry.Description);
                }
                p.AddAttribute(typeof(DebuggerNonUserCodeAttribute));
                if (specialSetting != null)
                {
                    p.AddAttribute(typeof(SpecialSettingAttribute),
                                   Easy.Prim(specialSetting.Value));
                }
                if (entry.GenerateDefaultValueInCode)
                {
                    p.AddAttribute(typeof(DefaultSettingValueAttribute), Easy.Prim(entry.SerializedValue));
                }
                if (entry.Scope == SettingScope.User && entry.Roaming)
                {
                    p.AddAttribute(typeof(SettingsManageabilityAttribute),
                                   Easy.Prim(SettingsManageability.Roaming));
                }
                p.Getter.Return(Easy.This.Index(Easy.Prim(entry.Name)).CastTo(entryType));
//				p.GetStatements.Add(new CodeMethodReturnStatement(
//					new CodeCastExpression(new CodeTypeReference(entryType),
//					                       new CodeIndexerExpression(new CodeThisReferenceExpression(),
//					                                                 new CodePrimitiveExpression(entry.Name))
//					                      )
//				));
                if (entry.Scope == SettingScope.User)
                {
                    p.Setter.Assign(Easy.This.Index(Easy.Prim(entry.Name)), Easy.Value);
                }
            }

            return(c);
        }
示例#5
0
            private void GenerateTypeConverter(CodeTypeDeclaration generatedType, Dictionary <ILiteral, string> fieldNames)
            {
                var dependents = generatedType.DependentTypes(true);

                if (dependents.Any(c => c.Name == generatedType.Name + "Converter") ||
                    fieldNames.All(field => field.Value == field.Key.Name))
                {
                    return;
                }

                var typeConverter = new CodeTypeDeclaration(generatedType.Name + "Converter");

                typeConverter.BaseTypes.Add(typeof(TypeConverter).ToTypeReference());
                var stringTypeRef = new CodeTypeOfExpression(typeof(string));

                var canConvertFromMethod = new CodeMemberMethod
                {
                    Name       = "CanConvertFrom",
                    Attributes = MemberAttributes.Public | MemberAttributes.Override,
                    ReturnType = new CodeTypeReference(typeof(bool))
                };

                canConvertFromMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(ITypeDescriptorContext).ToTypeReference(), "context"));
                canConvertFromMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(System.Type).ToTypeReference(), "sourceType"));
                var sourceTypeRef = new CodeArgumentReferenceExpression("sourceType");

                canConvertFromMethod.Statements.Add(new CodeMethodReturnStatement(new CodeBinaryOperatorExpression(sourceTypeRef, CodeBinaryOperatorType.IdentityEquality, stringTypeRef)));
                typeConverter.Members.Add(canConvertFromMethod);

                var canConvertTo = new CodeMemberMethod
                {
                    Name       = "CanConvertTo",
                    Attributes = MemberAttributes.Public | MemberAttributes.Override,
                    ReturnType = new CodeTypeReference(typeof(bool))
                };

                canConvertTo.Parameters.Add(new CodeParameterDeclarationExpression(typeof(ITypeDescriptorContext).ToTypeReference(), "context"));
                canConvertTo.Parameters.Add(new CodeParameterDeclarationExpression(typeof(System.Type), "destinationType"));
                var destinationTypeRef = new CodeArgumentReferenceExpression("destinationType");

                canConvertTo.Statements.Add(new CodeMethodReturnStatement(new CodeBinaryOperatorExpression(destinationTypeRef, CodeBinaryOperatorType.IdentityEquality, stringTypeRef)));
                typeConverter.Members.Add(canConvertTo);

                var typeRef        = generatedType.GetReferenceForType();
                var typeExpression = new CodeTypeReferenceExpression(typeRef);
                var convertFrom    = new CodeMemberMethod
                {
                    Name       = "ConvertFrom",
                    Attributes = MemberAttributes.Public | MemberAttributes.Override,
                    ReturnType = new CodeTypeReference(typeof(object))
                };

                convertFrom.Parameters.Add(new CodeParameterDeclarationExpression(typeof(ITypeDescriptorContext).ToTypeReference(), "context"));
                convertFrom.Parameters.Add(new CodeParameterDeclarationExpression(typeof(CultureInfo).ToTypeReference(), "culture"));
                convertFrom.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "value"));
                var valueRef = new CodeArgumentReferenceExpression("value");
                var nullRef  = new CodePrimitiveExpression();

                convertFrom.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(valueRef, CodeBinaryOperatorType.IdentityEquality, nullRef),
                                                                      new CodeMethodReturnStatement(new CodeDefaultValueExpression(typeRef))));
                var valueString = new CodeVariableDeclarationStatement(typeof(string), "valueString", new CodeMethodInvokeExpression(valueRef, "ToString"));

                convertFrom.Statements.Add(valueString);
                var valueStringRef = new CodeVariableReferenceExpression(valueString.Name);

                foreach (var field in fieldNames)
                {
                    convertFrom.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(valueStringRef, CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(field.Key.Name)),
                                                                          new CodeMethodReturnStatement(new CodeFieldReferenceExpression(typeExpression, field.Value))));
                }
                convertFrom.Statements.Add(new CodeMethodReturnStatement(new CodeDefaultValueExpression(typeRef)));
                typeConverter.Members.Add(convertFrom);

                var convertTo = new CodeMemberMethod
                {
                    Name       = "ConvertTo",
                    Attributes = MemberAttributes.Public | MemberAttributes.Override,
                    ReturnType = new CodeTypeReference(typeof(object))
                };

                convertTo.Parameters.Add(new CodeParameterDeclarationExpression(typeof(ITypeDescriptorContext).ToTypeReference(), "context"));
                convertTo.Parameters.Add(new CodeParameterDeclarationExpression(typeof(CultureInfo).ToTypeReference(), "culture"));
                convertTo.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "value"));
                convertTo.Parameters.Add(new CodeParameterDeclarationExpression(typeof(System.Type).ToTypeReference(), "destinationType"));
                convertTo.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(valueRef, CodeBinaryOperatorType.IdentityEquality, nullRef),
                                                                    new CodeMethodReturnStatement(nullRef)));
                var valueCasted = new CodeVariableDeclarationStatement(typeRef, "valueCasted", new CodeCastExpression(typeRef, valueRef));

                convertTo.Statements.Add(valueCasted);
                var valueCastedRef = new CodeVariableReferenceExpression(valueCasted.Name);

                foreach (var field in fieldNames)
                {
                    convertTo.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(valueCastedRef, CodeBinaryOperatorType.ValueEquality, new CodeFieldReferenceExpression(typeExpression, field.Value)),
                                                                        new CodeMethodReturnStatement(new CodePrimitiveExpression(field.Key.Name))));
                }
                convertTo.ThrowException <ArgumentOutOfRangeException>("value");
                typeConverter.Members.Add(convertTo);

                generatedType.AddAttribute(typeof(TypeConverterAttribute), new CodeTypeOfExpression(typeConverter.Name));
                dependents.Add(typeConverter);
            }