private static CodeAttributeDeclaration CodeForNameIndexAttribute(Type type, string name, int index)
 {
     CodeTypeReference indexAttributeType = new CodeTypeReference(type);
     CodeAttributeArgument nameAttribute = new CodeAttributeArgument(new CodePrimitiveExpression(name));
     CodeAttributeArgument indexAttribute = new CodeAttributeArgument(new CodePrimitiveExpression(index));
     return new CodeAttributeDeclaration(indexAttributeType, nameAttribute, indexAttribute);
 }
 protected override void GenerateClassAttributes()
 {
     base.GenerateClassAttributes();
     if ((base._sourceDataClass != null) && (this.Parser.OutputCacheParameters != null))
     {
         OutputCacheParameters outputCacheParameters = this.Parser.OutputCacheParameters;
         if (outputCacheParameters.Duration > 0)
         {
             CodeAttributeDeclaration declaration = new CodeAttributeDeclaration("System.Web.UI.PartialCachingAttribute");
             CodeAttributeArgument argument = new CodeAttributeArgument(new CodePrimitiveExpression(outputCacheParameters.Duration));
             declaration.Arguments.Add(argument);
             argument = new CodeAttributeArgument(new CodePrimitiveExpression(outputCacheParameters.VaryByParam));
             declaration.Arguments.Add(argument);
             argument = new CodeAttributeArgument(new CodePrimitiveExpression(outputCacheParameters.VaryByControl));
             declaration.Arguments.Add(argument);
             argument = new CodeAttributeArgument(new CodePrimitiveExpression(outputCacheParameters.VaryByCustom));
             declaration.Arguments.Add(argument);
             argument = new CodeAttributeArgument(new CodePrimitiveExpression(outputCacheParameters.SqlDependency));
             declaration.Arguments.Add(argument);
             argument = new CodeAttributeArgument(new CodePrimitiveExpression(this.Parser.FSharedPartialCaching));
             declaration.Arguments.Add(argument);
             if (MultiTargetingUtil.IsTargetFramework40OrAbove)
             {
                 argument = new CodeAttributeArgument("ProviderName", new CodePrimitiveExpression(this.Parser.Provider));
                 declaration.Arguments.Add(argument);
             }
             base._sourceDataClass.CustomAttributes.Add(declaration);
         }
     }
 }
Exemplo n.º 3
0
		public void DefaultConstructor ()
		{
			CodeAttributeArgument caa = new CodeAttributeArgument ();
			Assert.IsNotNull (caa.Name, "#1");
			Assert.AreEqual (string.Empty, caa.Name, "#2");
			Assert.IsNull (caa.Value, "#3");
		}
Exemplo n.º 4
0
 public DynaAttributeDeclaration(string AttributeName, object[] AttributeArguements)
 {
     CodeAttributeArgument[] caa = new CodeAttributeArgument[AttributeArguements.Length];
     for (int i = 0; i < caa.Length; i++)
         caa[i] = new CodeAttributeArgument(new CodePrimitiveExpression(AttributeArguements[i]));
     cad = new CodeAttributeDeclaration(AttributeName, caa);
 }
    /*
     * Add metadata attributes to the class
     */
    protected override void GenerateClassAttributes() {

        base.GenerateClassAttributes();

        // If the user control has an OutputCache directive, generate
        // an attribute with the information about it.
        if (_sourceDataClass != null && Parser.OutputCacheParameters != null) {
            OutputCacheParameters cacheSettings = Parser.OutputCacheParameters;
            if (cacheSettings.Duration > 0) {
                CodeAttributeDeclaration attribDecl = new CodeAttributeDeclaration(
                    "System.Web.UI.PartialCachingAttribute");
                CodeAttributeArgument attribArg = new CodeAttributeArgument(
                    new CodePrimitiveExpression(cacheSettings.Duration));
                attribDecl.Arguments.Add(attribArg);
                attribArg = new CodeAttributeArgument(new CodePrimitiveExpression(cacheSettings.VaryByParam));
                attribDecl.Arguments.Add(attribArg);
                attribArg = new CodeAttributeArgument(new CodePrimitiveExpression(cacheSettings.VaryByControl));
                attribDecl.Arguments.Add(attribArg);
                attribArg = new CodeAttributeArgument(new CodePrimitiveExpression(cacheSettings.VaryByCustom));
                attribDecl.Arguments.Add(attribArg);
                attribArg = new CodeAttributeArgument(new CodePrimitiveExpression(cacheSettings.SqlDependency));
                attribDecl.Arguments.Add(attribArg);
                attribArg = new CodeAttributeArgument(new CodePrimitiveExpression(Parser.FSharedPartialCaching));
                attribDecl.Arguments.Add(attribArg);
                // Use the providerName argument only when targeting 4.0 and above.
                if (MultiTargetingUtil.IsTargetFramework40OrAbove) {
                    attribArg = new CodeAttributeArgument("ProviderName", new CodePrimitiveExpression(Parser.Provider));
                    attribDecl.Arguments.Add(attribArg);
                }

                _sourceDataClass.CustomAttributes.Add(attribDecl);
            }
        }
    }
		public void Constructor1_NullItem ()
		{
			CodeAttributeArgument[] arguments = new CodeAttributeArgument[] { 
				new CodeAttributeArgument (), null };

			CodeAttributeArgumentCollection coll = new CodeAttributeArgumentCollection (
				arguments);
		}
 /// <devdoc>
 /// <para>Copies the elements of an array to the end of the <see cref='System.CodeDom.CodeAttributeArgumentCollection'/>.</para>
 /// </devdoc>
 public void AddRange(CodeAttributeArgument[] value) {
     if (value == null) {
         throw new ArgumentNullException("value");
     }
     for (int i = 0; ((i) < (value.Length)); i = ((i) + (1))) {
         this.Add(value[i]);
     }
 }
 public static CodeAttributeArgument Clone(this CodeAttributeArgument argument)
 {
     if (argument == null) return null;
     CodeAttributeArgument a = new CodeAttributeArgument();
     a.Name = argument.Name;
     a.Value = argument.Value.Clone();
     return a;
 }
Exemplo n.º 9
0
		public void Constructor0_Deny_Unrestricted ()
		{
			CodeAttributeArgument caa = new CodeAttributeArgument ();
			Assert.AreEqual (String.Empty, caa.Name, "Name");
			caa.Name = null;
			Assert.IsNull (caa.Value, "Value");
			caa.Value = new CodeExpression ();
		}
Exemplo n.º 10
0
		public void Constructor2_Deny_Unrestricted ()
		{
			CodeExpression value = new CodeExpression ();
			CodeAttributeArgument caa = new CodeAttributeArgument ("mono", value);
			Assert.AreEqual ("mono", caa.Name, "Name");
			caa.Name = String.Empty;
			Assert.AreSame (value, caa.Value, "Value");
			caa.Value = new CodeExpression ();
		}
Exemplo n.º 11
0
        public void AddAttribute(CodeTypeMember member, string attributeType,
            string expression = null)
        {
            // Add [attributeType(expression)]
            var argument = new CodeAttributeArgument(new CodeSnippetExpression(expression));
            var attribute = new CodeAttributeDeclaration(attributeType, argument);

            member.CustomAttributes.Add(attribute);
        }
Exemplo n.º 12
0
		public void Constructor2_Deny_Unrestricted ()
		{
			CodeAttributeArgument[] args = new CodeAttributeArgument[1] { new CodeAttributeArgument () };
			CodeAttributeDeclaration cad = new CodeAttributeDeclaration ("mono", args);
			Assert.AreEqual (1, cad.Arguments.Count, "Arguments");
			Assert.AreEqual ("mono", cad.Name, "Name");
			cad.Name = null;
			Assert.AreEqual ("System.Void", cad.AttributeType.BaseType, "AttributeType.BaseType");
		}
        private static void AddXmlEnumAttribute(CodeTypeMember member)
        {
            CodeTypeReference attributeType = new CodeTypeReference(typeof (XmlEnumAttribute));
            CodePrimitiveExpression argumentValue = new CodePrimitiveExpression(member.Name);
            CodeAttributeArgument argument = new CodeAttributeArgument(argumentValue);
            CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(attributeType, argument);

            member.CustomAttributes.Add(attribute);
        }
Exemplo n.º 14
0
 internal static void AddCustomAttribute(CodeTypeDeclaration type, string AttributeName, string ArgumentName, object ArgumentValue)
 {
     CodeAttributeDeclaration cad = new CodeAttributeDeclaration(AttributeName);
     CodeAttributeArgument cadarg = new CodeAttributeArgument();
     if (ArgumentName != String.Empty)
         cadarg.Name = ArgumentName;
     cadarg.Value = new CodePrimitiveExpression(ArgumentValue);
     cad.Arguments.Add(cadarg);
     type.CustomAttributes.Add(cad);
 }
		public void AddRange (CodeAttributeArgument [] value )
		{
			if (value == null) {
				throw new ArgumentNullException ("value");
			}

			for (int i = 0; i < value.Length; i++) {
				Add (value[i]);
			}
		}
 private static void AddRootElementName(CodeTypeMember type)
 {
     foreach (CodeAttributeDeclaration attribute in type.CustomAttributes)
         if (attribute.Name == typeof (XmlRootAttribute).FullName)
         {
             CodePrimitiveExpression value = new CodePrimitiveExpression(type.Name);
             CodeAttributeArgument argument = new CodeAttributeArgument("", value);
             attribute.Arguments.Insert(0, argument);
         }
 }
 private static void AddGeneratedCodeAttributeforMember(CodeTypeMember typeMember)
 {
     CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(new CodeTypeReference(typeof(GeneratedCodeAttribute))) {
         AttributeType = { Options = CodeTypeReferenceOptions.GlobalReference }
     };
     CodeAttributeArgument argument = new CodeAttributeArgument(new CodePrimitiveExpression(typeof(StronglyTypedResourceBuilder).FullName));
     CodeAttributeArgument argument2 = new CodeAttributeArgument(new CodePrimitiveExpression("4.0.0.0"));
     declaration.Arguments.Add(argument);
     declaration.Arguments.Add(argument2);
     typeMember.CustomAttributes.Add(declaration);
 }
Exemplo n.º 18
0
		public void NullName ()
		{
			CodeAttributeArgument caa = new CodeAttributeArgument ((string) null, (CodeExpression) null);
			Assert.IsNotNull (caa.Name, "#1");
			Assert.AreEqual (string.Empty, caa.Name, "#2");
			Assert.IsNull (caa.Value, "#3");

			caa.Name = null;
			Assert.IsNotNull (caa.Name, "#4");
			Assert.AreEqual (string.Empty, caa.Name, "#5");
		}
		public void Constructor1 ()
		{
			CodeAttributeArgument caa1 = new CodeAttributeArgument ();
			CodeAttributeArgument caa2 = new CodeAttributeArgument ();

			CodeAttributeArgument[] arguments = new CodeAttributeArgument[] { caa1, caa2 };
			CodeAttributeArgumentCollection coll = new CodeAttributeArgumentCollection (
				arguments);

			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (caa1), "#2");
			Assert.AreEqual (1, coll.IndexOf (caa2), "#3");
		}
		public void Constructor2 ()
		{
			CodeAttributeArgument caa1 = new CodeAttributeArgument ();
			CodeAttributeArgument caa2 = new CodeAttributeArgument ();

			CodeAttributeArgumentCollection c = new CodeAttributeArgumentCollection ();
			c.Add (caa1);
			c.Add (caa2);

			CodeAttributeArgumentCollection coll = new CodeAttributeArgumentCollection (c);
			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (caa1), "#2");
			Assert.AreEqual (1, coll.IndexOf (caa2), "#3");
		}
Exemplo n.º 21
0
        public CodeCompileUnit GetCompileUnit(string className)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className);

            var mapper = new DataTypeMapper();
            var newType = compileUnit.Namespaces[0].Types[0];

            var nameArgument = new CodeAttributeArgument("Name", new CodeSnippetExpression("\"" + className + "\" "));
            var nameSpaceArgument = new CodeAttributeArgument("Namespace", new CodeSnippetExpression("\"\""));
            newType.CustomAttributes = new CodeAttributeDeclarationCollection {new CodeAttributeDeclaration("DataContract", nameArgument, nameSpaceArgument)};

            foreach (var column in table.Columns)
            {
                if(column.IsPrimaryKey)
                {
                    if(appPrefs.IncludeHasMany)
                        foreach (var foreignKeyTable in table.HasManyRelationships)
                        {
                            var fkEntityName = appPrefs.ClassNamePrefix + foreignKeyTable.Reference.MakeSingular().GetPreferenceFormattedText(appPrefs);
                            newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute("IList<" + fkEntityName + ">", foreignKeyTable.Reference.MakePlural().GetPreferenceFormattedText(appPrefs)));
                        }

                    var primaryKeyType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale);
                    newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(primaryKeyType.Name, "Id"));
                    continue;
                }
                if (column.IsForeignKey)
                {
                    var fKey = column.ForeignKeyTableName;
                    var typeName = appPrefs.ClassNamePrefix + fKey.MakeSingular().GetPreferenceFormattedText(appPrefs);
                    var codeMemberProperty = codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(typeName, fKey.MakeSingular().GetPreferenceFormattedText(appPrefs));
                    newType.Members.Add(codeMemberProperty);
                    continue;
                }
                var propertyName = column.Name.GetPreferenceFormattedText(appPrefs);
                var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale);

                newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(mapFromDbType.Name, propertyName));
            }
            return compileUnit;
        }
Exemplo n.º 22
0
        static CodeMemberProperty CreateProperty(PropertyNameType nameType, string constantsClassName, string defaultValuesClassName)
        {
            CodeMemberProperty publicProp = new CodeMemberProperty();
            publicProp.Name = nameType.propertyName;
            publicProp.Attributes = CodeDomHelperObjects.PublicFinal;
            publicProp.HasGet = true;
            publicProp.HasSet = true;
            publicProp.Type = new CodeTypeReference(nameType.propertyType);

            CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection();
            CodeAttributeArgument arg1 = new CodeAttributeArgument(
                                            new CodeFieldReferenceExpression(
                                                new CodeTypeReferenceExpression(constantsClassName),
                                                nameType.propertyName));
            CodeAttributeArgument arg2 = new CodeAttributeArgument(
                                            PropertyNameConstants.DefaultValueProperty,
                                            new CodeFieldReferenceExpression(
                                                new CodeTypeReferenceExpression(defaultValuesClassName),
                                                Constants.DefaultPrefix + nameType.propertyName));
            // Future TODO: Provide a library with attributes that custom channel writes 
            // can use to adorn their properties with default values and validators, we can
            // then add it here.
            attributes.Add(new CodeAttributeDeclaration(
                            new CodeTypeReference(TypeNameConstants.ConfigurationProperty),
                            arg1, arg2));
            publicProp.CustomAttributes = new CodeAttributeDeclarationCollection(attributes);
            string nameInConfig = constantsClassName + "." + nameType.propertyName;
            CodeArrayIndexerExpression baseIndexedProperty
                                        = new CodeArrayIndexerExpression(
                                            CodeDomHelperObjects.BaseRef,
                                            new CodeFieldReferenceExpression(
                                                new CodeTypeReferenceExpression(constantsClassName),
                                                nameType.propertyName));
            publicProp.GetStatements.Add(new CodeMethodReturnStatement(
                                                new CodeCastExpression(
                                                    nameType.propertyType,
                                                    baseIndexedProperty)));
            publicProp.SetStatements.Add(new CodeAssignStatement(
                                                baseIndexedProperty,
                                                new CodePropertySetValueReferenceExpression()));
            return publicProp;
        }
Exemplo n.º 23
0
 protected override void OutputAttributeArgument(CodeAttributeArgument arg)
 {
     if ((arg.Name != null) && (arg.Name.Length > 0))
     {
         this.OutputIdentifier(arg.Name);
         base.Output.Write(":=");
     }
     ((ICodeGenerator)this).GenerateCodeFromExpression(arg.Value, ((IndentedTextWriter)base.Output).InnerWriter, base.Options);
 }
Exemplo n.º 24
0
 public int Add(CodeAttributeArgument value) => List.Add(value);
		//
		// Methods
		//
		public int Add (CodeAttributeArgument value)
		{
			return List.Add( value );
		}
		public CodeAttributeArgumentCollection( CodeAttributeArgument[] value )
		{
			AddRange( value );
		}
Exemplo n.º 27
0
 public bool Contains(CodeAttributeArgument value)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 28
0
 public int IndexOf(CodeAttributeArgument value)
 {
     throw new NotImplementedException();
 }
 /// <include file='doc\CodeAttributeArgumentCollection.uex' path='docs/doc[@for="CodeAttributeArgumentCollection.Remove"]/*' />
 /// <devdoc>
 ///    <para> Removes a specific <see cref='System.CodeDom.CodeAttributeArgument'/> from the
 ///    <see cref='System.CodeDom.CodeAttributeArgumentCollection'/> .</para>
 /// </devdoc>
 public void Remove(CodeAttributeArgument value)
 {
     List.Remove(value);
 }
Exemplo n.º 30
0
 public int IndexOf(CodeAttributeArgument value) => List.IndexOf(value);
        /// <summary>
        /// Generates the code to store the views in a C# or a VB file based on the
        /// options passed in by the user.
        /// </summary>
        /// <param name="mappingCollection"></param>
        /// <param name="generatedViews"></param>
        /// <param name="sourceWriter"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        private static void GenerateAndStoreViews(StorageMappingItemCollection mappingCollection,
            Dictionary<EntitySetBase, string> generatedViews, TextWriter sourceWriter, CodeDomProvider provider, IList<EdmSchemaError> schemaErrors)
        {
            EdmItemCollection edmCollection = mappingCollection.EdmItemCollection;
            StoreItemCollection storeCollection = mappingCollection.StoreItemCollection;

            //Create an emtpty compile unit and build up the generated code
            CodeCompileUnit compileUnit = new CodeCompileUnit();

            //Add the namespace for generated code
            CodeNamespace codeNamespace = new CodeNamespace(EntityViewGenerationConstants.NamespaceName);
            //Add copyright notice to the namespace comment.
            compileUnit.Namespaces.Add(codeNamespace);

            foreach (var storageEntityContainerMapping in mappingCollection.GetItems<StorageEntityContainerMapping>())
            {
                //Throw warning when containerMapping contains query view for bug 547285.
                if (HasQueryView(storageEntityContainerMapping))
                {
                    schemaErrors.Add(new EdmSchemaError(
                        Strings.UnsupportedQueryViewInEntityContainerMapping(storageEntityContainerMapping.Identity), 
                        (int)StorageMappingErrorCode.UnsupportedQueryViewInEntityContainerMapping, 
                        EdmSchemaErrorSeverity.Warning));
                    continue;
                }

                #region Class Declaration

                string edmContainerName = storageEntityContainerMapping.EdmEntityContainer.Name;
                string storeContainerName = storageEntityContainerMapping.StorageEntityContainer.Name;

                string hashOverMappingClosure = MetadataMappingHasherVisitor.GetMappingClosureHash(edmCollection.EdmVersion, storageEntityContainerMapping);

                StringBuilder inputForTypeNameContent = new StringBuilder(hashOverMappingClosure);

                string viewStorageTypeName = EntityViewGenerationConstants.ViewGenerationTypeNamePrefix + StringHashBuilder.ComputeHash(MetadataHelper.CreateMetadataHashAlgorithm(edmCollection.EdmVersion),  inputForTypeNameContent.ToString()).ToUpperInvariant();

                //Add typeof expression to get the type that contains ViewGen type. This will help us in avoiding to go through 
                //all the types in the assembly. I have also verified that this works with VB with a root namespace prepended to the
                //namespace since VB is picking up the type correctly as long as it is in the same assembly even with out the root namespace.
                CodeTypeOfExpression viewGenTypeOfExpression = new CodeTypeOfExpression(EntityViewGenerationConstants.NamespaceName + "." + viewStorageTypeName);
                //Add the assembly attribute that marks the assembly as the one that contains the generated views
                CodeAttributeDeclaration viewGenAttribute = new CodeAttributeDeclaration(EntityViewGenerationConstants.ViewGenerationCustomAttributeName);
                CodeAttributeArgument viewGenTypeArgument = new CodeAttributeArgument(viewGenTypeOfExpression);
                viewGenAttribute.Arguments.Add(viewGenTypeArgument);
                compileUnit.AssemblyCustomAttributes.Add(viewGenAttribute);

                //Add the type which will be the class that contains all the views in this assembly
                CodeTypeDeclaration viewStoringType = CreateTypeForStoringViews(viewStorageTypeName);

                //Add the constructor, this will be the only method that this type will contain
                //Create empty constructor.
                CodeConstructor viewStoringTypeConstructor = CreateConstructorForViewStoringType();
                viewStoringType.Attributes = MemberAttributes.Public;


                //Get an expression that expresses this instance
                CodeThisReferenceExpression thisRef = new CodeThisReferenceExpression();


                string viewHash = MetadataHelper.GenerateHashForAllExtentViewsContent(edmCollection.EdmVersion, GenerateDictionaryForEntitySetNameAndView(generatedViews));


                CodeAssignStatement EdmEntityContainerNameStatement = 
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(thisRef, EntityViewGenerationConstants.EdmEntityContainerName), 
                        new CodePrimitiveExpression(edmContainerName));
                CodeAssignStatement StoreEntityContainerNameStatement =
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(thisRef, EntityViewGenerationConstants.StoreEntityContainerName),
                        new CodePrimitiveExpression(storeContainerName));
                CodeAssignStatement HashOverMappingClosureStatement =
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(thisRef, EntityViewGenerationConstants.HashOverMappingClosure),
                        new CodePrimitiveExpression(hashOverMappingClosure));
                CodeAssignStatement HashOverAllExtentViewsStatement =
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(thisRef, EntityViewGenerationConstants.HashOverAllExtentViews),
                        new CodePrimitiveExpression(viewHash));
                CodeAssignStatement ViewCountStatement =
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(thisRef, EntityViewGenerationConstants.ViewCountPropertyName),
                        new CodePrimitiveExpression(generatedViews.Count));

                viewStoringTypeConstructor.Statements.Add(EdmEntityContainerNameStatement);
                viewStoringTypeConstructor.Statements.Add(StoreEntityContainerNameStatement);
                viewStoringTypeConstructor.Statements.Add(HashOverMappingClosureStatement);
                viewStoringTypeConstructor.Statements.Add(HashOverAllExtentViewsStatement);
                viewStoringTypeConstructor.Statements.Add(ViewCountStatement);

                //Add the constructor to the type
                viewStoringType.Members.Add(viewStoringTypeConstructor);
                //Add the method to store views to the type
                CreateAndAddGetViewAtMethod(viewStoringType, generatedViews);

                //Add the type to the namespace
                codeNamespace.Types.Add(viewStoringType);

                #endregion
            }

            if (codeNamespace.Types.Count > 0)
            {
                GenerateCode(sourceWriter, provider, compileUnit);
                sourceWriter.Flush();
            }
        }
Exemplo n.º 32
0
        public override CodeTypeDeclaration Generate()
        {
            var classDeclaration = base.Generate();

            GenerateSerializableAttribute(classDeclaration);
            GenerateTypeAttribute(classDeclaration);

            classDeclaration.IsClass = true;
            classDeclaration.IsPartial = true;

            if (EnableDataBinding)
            {
                classDeclaration.Members.Add(new CodeMemberEvent()
                {
                    Name = "PropertyChanged",
                    Type = new CodeTypeReference("PropertyChangedEventHandler"),
                    Attributes = MemberAttributes.Public,
                });

                var onPropChangedMethod = new CodeMemberMethod
                {
                    Name = "OnPropertyChanged",
                    Attributes = MemberAttributes.Family,
                };
                var param = new CodeParameterDeclarationExpression(typeof(string), "propertyName");
                onPropChangedMethod.Parameters.Add(param);
                onPropChangedMethod.Statements.Add(
                    new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(
                            new CodeEventReferenceExpression(null, "PropertyChanged"),
                            CodeBinaryOperatorType.IdentityInequality,
                            new CodePrimitiveExpression(null)),
                        new CodeExpressionStatement(new CodeDelegateInvokeExpression(
                            new CodeEventReferenceExpression(null, "PropertyChanged"),
                            new CodeThisReferenceExpression(),
                            new CodeObjectCreateExpression(
                                "PropertyChangedEventArgs",
                                new CodeArgumentReferenceExpression("propertyName"))
                            ))));
                classDeclaration.Members.Add(onPropChangedMethod);
            }

            if (BaseClass != null)
            {
                if (BaseClass is ClassModel)
                    classDeclaration.BaseTypes.Add(BaseClass.GetReferenceFor(Namespace, false));
                else
                {
                    var typeReference = BaseClass.GetReferenceFor(Namespace, false);

                    var member = new CodeMemberField(typeReference, "Value")
                    {
                        Attributes = MemberAttributes.Public,
                    };

                    if (EnableDataBinding)
                    {
                        var backingFieldMember = new CodeMemberField(typeReference, member.Name.ToBackingField())
                        {
                            Attributes = MemberAttributes.Private
                        };
                        member.Name += PropertyModel.GetAccessors(member.Name, backingFieldMember.Name, BaseClass.GetPropertyValueTypeCode(), false);
                        classDeclaration.Members.Add(backingFieldMember);
                    }
                    else
                    {
                        // hack to generate automatic property
                        member.Name += " { get; set; }";
                    }

                    var docs = new[] { new DocumentationModel { Language = "en", Text = "Gets or sets the text value." },
                        new DocumentationModel { Language = "de", Text = "Ruft den Text ab oder legt diesen fest." } };
                    member.Comments.AddRange(DocumentationModel.GetComments(docs).ToArray());

                    var attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlTextAttribute)));
                    var simpleModel = BaseClass as SimpleModel;
                    if (simpleModel != null && (simpleModel.XmlSchemaType.IsDataTypeAttributeAllowed(Configuration) ?? simpleModel.UseDataTypeAttribute))
                    {
                        var name = BaseClass.GetQualifiedName();
                        if (name.Namespace == XmlSchema.Namespace)
                        {
                            var dataType = new CodeAttributeArgument("DataType", new CodePrimitiveExpression(name.Name));
                            attribute.Arguments.Add(dataType);
                        }
                    }

                    member.CustomAttributes.Add(attribute);
                    classDeclaration.Members.Add(member);
                }
            }

            if (EnableDataBinding)
                classDeclaration.BaseTypes.Add(new CodeTypeReference("INotifyPropertyChanged"));

            if (Configuration.EntityFramework && (BaseClass == null || !(BaseClass is ClassModel)))
            {
                // generate key
                var keyProperty = Properties.FirstOrDefault(p => p.Name.ToLowerInvariant() == "id")
                    ?? Properties.FirstOrDefault(p => p.Name.ToLowerInvariant() == (Name.ToLowerInvariant() + "id"));

                if (keyProperty == null)
                {
                    keyProperty = new PropertyModel(Configuration)
                    {
                        Name = "Id",
                        Type = new SimpleModel(Configuration) { ValueType = typeof(long) },
                        OwningType = this,
                        Documentation = { new DocumentationModel {  Language = "en", Text = "Gets or sets a value uniquely identifying this entity." },
                            new DocumentationModel { Language = "de", Text = "Ruft einen Wert ab, der diese Entität eindeutig identifiziert, oder legt diesen fest." } }
                    };
                    Properties.Insert(0, keyProperty);
                }

                keyProperty.IsKey = true;
            }

            foreach (var property in Properties)
                property.AddMembersTo(classDeclaration, EnableDataBinding);

            if (IsMixed && (BaseClass == null || BaseClass is ClassModel))
            {
                var text = new CodeMemberField(typeof(string), "Text");
                // hack to generate automatic property
                text.Name += " { get; set; }";
                text.Attributes = MemberAttributes.Public;
                var xmlTextAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlTextAttribute)));
                text.CustomAttributes.Add(xmlTextAttribute);
                classDeclaration.Members.Add(text);
            }

            classDeclaration.CustomAttributes.Add(
                new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerStepThroughAttribute))));
            if (Configuration.GenerateDesignerCategoryAttribute)
            {
                classDeclaration.CustomAttributes.Add(
                    new CodeAttributeDeclaration(new CodeTypeReference(typeof (DesignerCategoryAttribute)),
                        new CodeAttributeArgument(new CodePrimitiveExpression("code"))));
            }

            if (RootElementName != null)
            {
                var rootAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlRootAttribute)),
                    new CodeAttributeArgument(new CodePrimitiveExpression(RootElementName.Name)),
                    new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(RootElementName.Namespace)));
                classDeclaration.CustomAttributes.Add(rootAttribute);
            }

            var derivedTypes = GetAllDerivedTypes();
            foreach (var derivedType in derivedTypes.OrderBy(t => t.Name))
            {
                var includeAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlIncludeAttribute)),
                    new CodeAttributeArgument(new CodeTypeOfExpression(derivedType.GetReferenceFor(Namespace, false))));
                classDeclaration.CustomAttributes.Add(includeAttribute);
            }

            classDeclaration.BaseTypes.AddRange(Interfaces.Select(i => i.GetReferenceFor(Namespace, false)).ToArray());

            return classDeclaration;
        }
 /// <include file='doc\CodeAttributeArgumentCollection.uex' path='docs/doc[@for="CodeAttributeArgumentCollection.Contains"]/*' />
 /// <devdoc>
 /// <para>Gets a value indicating whether the
 ///    <see cref='System.CodeDom.CodeAttributeArgumentCollection'/> contains the specified <see cref='System.CodeDom.CodeAttributeArgument'/>.</para>
 /// </devdoc>
 public bool Contains(CodeAttributeArgument value)
 {
     return(List.Contains(value));
 }
Exemplo n.º 34
0
 public void Remove(CodeAttributeArgument value)
 {
     throw new NotImplementedException();
 }
 /// <include file='doc\CodeAttributeArgumentCollection.uex' path='docs/doc[@for="CodeAttributeArgumentCollection.Add"]/*' />
 /// <devdoc>
 ///    <para>Adds a <see cref='System.CodeDom.CodeAttributeArgument'/> with the specified value to the
 ///    <see cref='System.CodeDom.CodeAttributeArgumentCollection'/> .</para>
 /// </devdoc>
 public int Add(CodeAttributeArgument value)
 {
     return(List.Add(value));
 }
Exemplo n.º 36
0
 private static void AddEditorBrowsableAttribute(CodeTypeMember ctmTarget)
 {
     CodeFieldReferenceExpression cfre = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EditorBrowsableState)), "Never");
     CodeAttributeArgument caa = new CodeAttributeArgument(cfre);
     ctmTarget.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(EditorBrowsableAttribute).FullName, caa));
 }
Exemplo n.º 37
0
        private CodeAttributeDeclaration GetAttribute(bool isArray)
        {
            CodeAttributeDeclaration attribute;

            if (IsKey && XmlSchemaName == null)
            {
                attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlIgnoreAttribute)));
                return attribute;
            }

            if (IsAttribute)
            {
                if (IsAny)
                {
                    attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlAnyAttributeAttribute)));
                }
                else
                {
                    attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlAttributeAttribute)),
                        new CodeAttributeArgument(new CodePrimitiveExpression(XmlSchemaName.Name)));
                }
            }
            else if (!isArray)
            {
                if (IsAny)
                {
                    attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlAnyElementAttribute)));
                }
                else
                {
                    attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlElementAttribute)),
                        new CodeAttributeArgument(new CodePrimitiveExpression(XmlSchemaName.Name)));
                    if (Order != null)
                        attribute.Arguments.Add(new CodeAttributeArgument("Order",
                            new CodePrimitiveExpression(Order.Value)));
                }
            }
            else
            {
                attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlArrayAttribute)),
                    new CodeAttributeArgument(new CodePrimitiveExpression(XmlSchemaName.Name)));
            }

            if (Form == XmlSchemaForm.Qualified)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(OwningType.XmlSchemaName.Namespace)));
            }
            else if (XmlNamespace != null)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(XmlNamespace)));
            }
            else if (!IsAny)
            {
                attribute.Arguments.Add(new CodeAttributeArgument("Form",
                    new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference(typeof(XmlSchemaForm))),
                        "Unqualified")));
            }

            if (IsNillable)
                attribute.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(true)));

            var simpleModel = Type as SimpleModel;
            if (simpleModel != null && simpleModel.UseDataTypeAttribute)
            {
                var name = Type.XmlSchemaType.GetQualifiedName();
                if (name.Namespace == XmlSchema.Namespace)
                {
                    var dataType = new CodeAttributeArgument("DataType", new CodePrimitiveExpression(name.Name));
                    attribute.Arguments.Add(dataType);
                }
            }

            return attribute;
        }
 /// <include file='doc\CodeAttributeArgumentCollection.uex' path='docs/doc[@for="CodeAttributeArgumentCollection.IndexOf"]/*' />
 /// <devdoc>
 ///    <para>Returns the index of a <see cref='System.CodeDom.CodeAttributeArgument'/> in
 ///       the <see cref='System.CodeDom.CodeAttributeArgumentCollection'/> .</para>
 /// </devdoc>
 public int IndexOf(CodeAttributeArgument value)
 {
     return(List.IndexOf(value));
 }
Exemplo n.º 39
0
 internal static CodeAttributeDeclaration AddAttribute(CodeAttributeDeclarationCollection col,
     CodeTypeReference type,
     CodeExpression[] arguments)
 {
     CodeAttributeArgument[] attributeArguments = new CodeAttributeArgument[arguments.Length];
     for (int i = 0; i < arguments.Length; i++) {
         attributeArguments[i] = new CodeAttributeArgument(arguments[i]);
     }
     CodeAttributeDeclaration cad = new CodeAttributeDeclaration(type, attributeArguments);
     col.Add(cad);
     return cad;
 }
Exemplo n.º 40
0
 public void Insert(int index, CodeAttributeArgument value)
 {
     throw new NotImplementedException();
 }
 /// <include file='doc\CodeAttributeArgumentCollection.uex' path='docs/doc[@for="CodeAttributeArgumentCollection.Insert"]/*' />
 /// <devdoc>
 /// <para>Inserts a <see cref='System.CodeDom.CodeAttributeArgument'/> into the <see cref='System.CodeDom.CodeAttributeArgumentCollection'/> at the specified index.</para>
 /// </devdoc>
 public void Insert(int index, CodeAttributeArgument value)
 {
     List.Insert(index, value);
 }
Exemplo n.º 42
0
 public bool Contains(CodeAttributeArgument value) => List.Contains(value);