Пример #1
0
		public void Deny_Unrestricted ()
		{
			VBCodeProvider vbprov = new VBCodeProvider ();
			Assert.AreEqual ("vb", vbprov.FileExtension, "FileExtension");
			Assert.AreEqual (LanguageOptions.CaseInsensitive, vbprov.LanguageOptions, "LanguageOptions");
			Assert.IsNotNull (vbprov.CreateCompiler (), "CreateCompiler");
			Assert.IsNotNull (vbprov.CreateGenerator (), "CreateGenerator");
			try {
				Assert.IsNotNull (vbprov.GetConverter (typeof (string)), "GetConverter");
			}
			catch (NotImplementedException) {
				// mono
			}
#if NET_2_0
			CodeTypeMember ctm = new CodeTypeMember ();
			StringWriter sw = new StringWriter ();
			CodeGeneratorOptions cgo = new CodeGeneratorOptions ();
			try {
				vbprov.GenerateCodeFromMember (ctm, sw, cgo);
			}
			catch (NotImplementedException) {
				// mono
			}
#endif
		}
Пример #2
0
 public static CodeAttributeDeclaration AddAttribute(CodeTypeMember tgtMethodCLR, string attr)
 {
     var declaration =
         new CodeAttributeDeclaration(new CodeTypeReference(attr, CodeTypeReferenceOptions.GlobalReference));
     tgtMethodCLR.CustomAttributes.Add(declaration);
     return declaration;
 }
Пример #3
0
		/// <summary>
		/// Creation method.
		/// </summary>
		/// <param name="className">Class name</param>
		/// <param name="isGet">isGet flag</param>
		/// <param name="member">Member</param>
		/// <param name="statement">Statement</param>
		/// <returns></returns>
		/// <remarks>For properties.</remarks>
		public static MethodInfo CreateProperty(string className,
		                                        bool isGet,
												CodeTypeMember member,
		                                        CodeStatement statement)
		{
			return new MethodInfo(member, className, true, isGet, statement);
		}
        protected void AddDescription(CodeTypeMember type, IEntityDescription description)
        {
            if (String.IsNullOrWhiteSpace(description.Description))
                return;

            AddAttribute(type, "Description", description.Description);
        }
 /// <summary>
 /// Creates a new instance of CodeTypeMemberExtension class.
 /// </summary>
 /// <param name="extendObject">An object to be decorated by this instance.</param>
 public CodeTypeMemberExtension(CodeTypeMember extendedObject, CodeTypeExtension parent) 
     : base(extendedObject)
 {                                  
     if (typeof(CodeMemberField) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Field;
     }
     else if (typeof(CodeMemberMethod) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Method;
     }
     else if (typeof(CodeMemberProperty) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Property;
     }
     else if (typeof(CodeMemberEvent) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Event;
     }
     else if (typeof(CodeSnippetTypeMember) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Snippet;
     }
     else if (typeof(CodeConstructor) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Constructor;
     }
     else if (typeof(CodeTypeConstructor) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.StaticConstructor;
     }
     this.parent = parent;
 }
Пример #6
0
        private void ValidateTypeMember(CodeTypeMember e) {
            ValidateCommentStatements(e.Comments);
            ValidateCodeDirectives(e.StartDirectives);
            ValidateCodeDirectives(e.EndDirectives);
            if (e.LinePragma != null) ValidateLinePragmaStart(e.LinePragma);

            if (e is CodeMemberEvent) {
                ValidateEvent((CodeMemberEvent)e);
            } 
            else if (e is CodeMemberField) {
                ValidateField((CodeMemberField)e);
            } 
            else if (e is CodeMemberMethod) {
                ValidateMemberMethod((CodeMemberMethod)e);
            } 
            else if (e is CodeMemberProperty) {
                ValidateProperty((CodeMemberProperty)e);
            } 
            else if (e is CodeSnippetTypeMember) {
                ValidateSnippetMember((CodeSnippetTypeMember)e);
            } 
            else if (e is CodeTypeDeclaration) {
                ValidateTypeDeclaration((CodeTypeDeclaration)e);
            } 
            else {
                throw new ArgumentException(SR.GetString(SR.InvalidElementType, e.GetType().FullName), "e");
            }
        }
        public void Process(CodeNamespace code, System.Xml.Schema.XmlSchema schema)
        {
            foreach (CodeTypeDeclaration type in code.Types)
            {
                if (type.IsClass || type.IsStruct)
                {
                    // Copy the colletion to an array for safety. We will be
                    // changing this collection.
                    CodeTypeMember[] members = new CodeTypeMember[type.Members.Count];
                    type.Members.CopyTo(members, 0);

                    foreach (CodeTypeMember member in members)
                    {
                        // Process fields only.
                        if (member is CodeMemberField)
                        {
                            CodeMemberProperty prop = new CodeMemberProperty();
                            prop.Name = member.Name;

                            prop.Attributes = member.Attributes;
                            prop.Type = ((CodeMemberField)member).Type;

                            // Copy attributes from field to the property.
                            prop.CustomAttributes.AddRange(member.CustomAttributes);
                            member.CustomAttributes.Clear();

                            // Copy comments from field to the property.
                            prop.Comments.AddRange(member.Comments);
                            member.Comments.Clear();

                            // Modify the field.
                            member.Attributes = MemberAttributes.Private;
                            Char[] letters = member.Name.ToCharArray();
                            letters[0] = Char.ToLower(letters[0]);
                            member.Name = String.Concat("_", new string(letters));

                            prop.HasGet = true;
                            prop.HasSet = true;

                            // Add get/set statements pointing to field. Generates:
                            // return this._fieldname;
                            prop.GetStatements.Add(
                                new CodeMethodReturnStatement(
                                new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
                                member.Name)));
                            // Generates:
                            // this._fieldname = value;
                            prop.SetStatements.Add(
                                new CodeAssignStatement(
                                new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
                                member.Name),
                                new CodeArgumentReferenceExpression("value")));

                            // Finally add the property to the type
                            type.Members.Add(prop);
                        }
                    }
                }
            }
        }
Пример #8
0
		public static IMember GetCompatibleMemberInClass (ProjectDom ctx, IType cls, CodeTypeMember member)
		{
			//check for identical property names
			foreach (IProperty prop in cls.Properties) {
				if (string.Compare (prop.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) {
					EnsureClassExists (ctx, prop.ReturnType.FullName, GetValidRegion (prop));
					CodeMemberProperty memProp = member as CodeMemberProperty;
					if (memProp == null || !IsTypeCompatible (ctx, prop.ReturnType.FullName, memProp.Type.BaseType))
						throw new MemberExistsException (cls.FullName, MemberType.Property, member, GetValidRegion (prop), cls.CompilationUnit.FileName);
					return prop;
				}
			}
				
			//check for identical method names
			foreach (IMethod meth in cls.Methods) {
				if (string.Compare (meth.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) {
					EnsureClassExists (ctx, meth.ReturnType.FullName, GetValidRegion (meth));
					CodeMemberMethod memMeth = member as CodeMemberMethod;
					if (memMeth == null || !IsTypeCompatible (ctx, meth.ReturnType.FullName, memMeth.ReturnType.BaseType))
						throw new MemberExistsException (cls.FullName, MemberType.Method, member, GetValidRegion (meth), cls.CompilationUnit.FileName);
					return meth;
				}
			}
			
			//check for identical event names
			foreach (IEvent ev in cls.Events) {
				if (string.Compare (ev.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) {
					EnsureClassExists (ctx, ev.ReturnType.FullName, GetValidRegion (ev));
					CodeMemberEvent memEv = member as CodeMemberEvent;
					if (memEv == null || !IsTypeCompatible (ctx, ev.ReturnType.FullName, memEv.Type.BaseType))
						throw new MemberExistsException (cls.FullName, MemberType.Event, member, GetValidRegion (ev), cls.CompilationUnit.FileName);
					return ev;
				}
			}
				
			//check for identical field names
			foreach (IField field in cls.Fields) {
				if (string.Compare (field.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) {
					EnsureClassExists (ctx, field.ReturnType.FullName, GetValidRegion (field));
					CodeMemberField memField = member as CodeMemberField;
					if (memField == null || !IsTypeCompatible (ctx, field.ReturnType.FullName, memField.Type.BaseType))
						throw new MemberExistsException (cls.FullName, MemberType.Field, member, GetValidRegion (field), cls.CompilationUnit.FileName);
					return field;
				}
			}
			
			//walk down into base classes, if any
			foreach (IReturnType baseType in cls.BaseTypes) {
				IType c = ctx.GetType (baseType);
				if (c == null)
					throw new TypeNotFoundException (baseType.FullName, cls.BodyRegion, cls.CompilationUnit.FileName);
				IMember mem = GetCompatibleMemberInClass (ctx, c, member);
				if (mem != null)
					return mem;
			}
			
			//return null if no match
			return null;
		}
Пример #9
0
 /// <devdoc>
 /// <para>Copies the elements of an array to the end of the <see cref='System.CodeDom.CodeTypeMemberCollection'/>.</para>
 /// </devdoc>
 public void AddRange(CodeTypeMember[] value) {
     if (value == null) {
         throw new ArgumentNullException("value");
     }
     for (int i = 0; ((i) < (value.Length)); i = ((i) + (1))) {
         this.Add(value[i]);
     }
 }
 protected static void AddDisplayNameIfDifferent(CodeTypeMember type, IEntityDescription description)
 {
     var name = description.Name;
     if (String.Compare(name, description.Alias, IgnoreCase) == 0 ||
         String.Compare(name, description.Alias.SplitPascalCase(), IgnoreCase) == 0)
         return;
     AddAttribute(type, "DisplayName", name);
 }
Пример #11
0
		public void Constructor1_NullItem ()
		{
			CodeTypeMember[] typeMembers = new CodeTypeMember[] { 
				new CodeTypeMember (), null };

			CodeTypeMemberCollection coll = new CodeTypeMemberCollection (
				typeMembers);
		}
 public TemplateMemberResult(ITemplateClassGenerator templateClass, MemberInfo memberInfo, GenerateMember memberAttribute, CodeTypeMember memberOutput, CodeTypeDeclaration decleration)
 {
     Decleration = decleration;
     TemplateClass = templateClass;
     MemberInfo = memberInfo;
     MemberAttribute = memberAttribute;
     MemberOutput = memberOutput;
 }
Пример #13
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);
        }
        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);
        }
        public virtual void Add(CodeTypeMember refAdd)
        {
            CodeTypeDeclaration typedeclAdd = refAdd as CodeTypeDeclaration;
            if (this.ns == null)
            {
                this.ns = CreateNamespace();
            }

            this.ns.Types.Add(typedeclAdd);
        }
		public void AddRange (CodeTypeMember [] 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 string GetRootElementName(CodeTypeMember type)
        {
            foreach (CodeAttributeDeclaration attribute in type.CustomAttributes)
                if (attribute.Name == typeof (XmlRootAttribute).FullName)
                    foreach (CodeAttributeArgument argument in attribute.Arguments)
                        if (argument.Name == "")
                            return ((CodePrimitiveExpression) argument.Value).Value.ToString();

            return null;
        }
Пример #19
0
        public static bool IsMetaDataGeneratable(CodeTypeMember member)
        {
            foreach (CodeAttributeDeclaration attribute in member.CustomAttributes)
            {
                if (attribute.Name == "PrimaryKey" || attribute.Name == "KeyProperty" || attribute.Name == "Field" || attribute.Name == "Property" || attribute.Name == "Version" || attribute.Name == "Timestamp")
                    return true;
            }

            return false;
        }
Пример #20
0
 private static QualifiedName GetUniqueName(CodeTypeMember member, QualifiedName parentName)
 {
     if (member is CodeTypeDeclaration)
     {
         return new QualifiedName(GetUniqueName((CodeTypeDeclaration)member), null);
     }
     else
     {
         return new QualifiedName(GetUniqueName(member), parentName);
     }
 }
 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);
 }
Пример #22
0
        public void BuildArgumentContextOfWrongTypeMemberShouldThrow()
        {
            var typeMember = new CodeTypeMember() { Name = "TestTypeMember" };
            var propData = mocks.Stub<IBuilderData>();
            Expect.Call(buildcontext.TypeMember).Return(typeMember);
            mocks.ReplayAll();

            Assert.Throws<ArgumentOutOfRangeException>(() => testObject.Build(this.buildcontext));

            mocks.VerifyAll();
        }
Пример #23
0
 private void ClearMember(CodeTypeMember member)
 {
     List<CodeCommentStatement> delcomm = new List<CodeCommentStatement>();
     foreach (CodeCommentStatement cmnt in member.Comments)
         if (cmnt.Comment.DocComment && cmnt.Comment.Text == "<remarks/>")
             delcomm.Add(cmnt);
     foreach (CodeCommentStatement delcom in delcomm)
         member.Comments.Remove(delcom);
     if (member is CodeTypeDeclaration)
         foreach (CodeTypeMember membermember in ((CodeTypeDeclaration)member).Members)
             ClearMember(membermember);
 }
 private static string GetXmlEnumValue(CodeTypeMember member)
 {
     if (member.CustomAttributes.Count == 1)
     {
         CodeAttributeArgument argument = member.CustomAttributes[0].Arguments[0];
         return ((CodePrimitiveExpression) argument.Value).Value.ToString();
     }
     else
     {
         AddXmlEnumAttribute(member);
         return null;
     }
 }
Пример #25
0
		public void Constructor1 ()
		{
			CodeTypeMember tm1 = new CodeTypeMember ();
			CodeTypeMember tm2 = new CodeTypeMember ();

			CodeTypeMember[] typeMembers = new CodeTypeMember[] { tm1, tm2 };
			CodeTypeMemberCollection coll = new CodeTypeMemberCollection (
				typeMembers);

			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (tm1), "#2");
			Assert.AreEqual (1, coll.IndexOf (tm2), "#3");
		}
Пример #26
0
 private static void EnumerareCodeMembers(CodeTypeMember member, QualifiedName parentName, Dictionary<string, CodeTypeMember> members)
 {
     QualifiedName memberName = GetUniqueName(member, parentName);
     members[memberName.ToString()] = member;
     CodeTypeDeclaration decl = member as CodeTypeDeclaration;
     if (decl != null)
     {
         foreach (CodeTypeMember subMember in decl.Members)
         {
             EnumerareCodeMembers(subMember, memberName, members);
         }
     }
 }
Пример #27
0
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="tag"></param>
		/// <param name="className"></param>
		public FieldInfo(CodeTypeMember tag, string className)
			: base(tag, className)
		{
			System.Diagnostics.Trace.Assert(tag != null);
			name = tag.Name;
			lineNumber = tag.LinePragma.LineNumber;
            CodeMemberProperty pro = tag as CodeMemberProperty;
			isProperty = pro != null;
			if (isProperty) {
				returnType = pro.Type.BaseType;
			} else {
				returnType = (tag as CodeMemberField).Type.BaseType;
			}
		}
Пример #28
0
		public void Constructor2 ()
		{
			CodeTypeMember tm1 = new CodeTypeMember ();
			CodeTypeMember tm2 = new CodeTypeMember ();

			CodeTypeMemberCollection c = new CodeTypeMemberCollection ();
			c.Add (tm1);
			c.Add (tm2);

			CodeTypeMemberCollection coll = new CodeTypeMemberCollection (c);
			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (tm1), "#2");
			Assert.AreEqual (1, coll.IndexOf (tm2), "#3");
		}
Пример #29
0
		public void Constructor0_Deny_Unrestricted ()
		{
			CodeTypeMember ctm = new CodeTypeMember ();
			Assert.AreEqual (MemberAttributes.Private | MemberAttributes.Final, ctm.Attributes, "Attributes");
			ctm.Attributes = MemberAttributes.Public;
			Assert.AreEqual (0, ctm.Comments.Count, "Comments");
			Assert.AreEqual (0, ctm.CustomAttributes.Count, "CustomAttributes");
			ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
			Assert.IsNull (ctm.LinePragma, "LinePragma");
			ctm.LinePragma = new CodeLinePragma (String.Empty, Int32.MaxValue);
			Assert.AreEqual (String.Empty, ctm.Name, "Name");
			ctm.Name = "mono";
			Assert.AreEqual (0, ctm.StartDirectives.Count, "StartDirectives");
			Assert.AreEqual (0, ctm.EndDirectives.Count, "EndDirectives");
		}
Пример #30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MemberBuildContext"/> class.
 /// </summary>
 /// <param name="codeNamespace">The code namespace of the test.</param>
 /// <param name="testClassDeclaration">The test class declaration.( early testObject ).</param>
 /// <param name="typeMember">The current type to create a test method for.</param>
 /// <param name="buildData">The additional build data lookup.</param>
 /// <param name="setUpTearDownContext">Contains data specific to SetUp and TearDown test-methods.</param>
 /// <param name="baseKey">The base string of the <see cref="MemberBuildContextBase.TestKey"/>. Is amended by the
 /// <paramref name="codeNamespace"/> identifier, normalized and fixed by a <see cref="KeynameFixer"/>.</param>
 public MemberBuildContext(
     CodeNamespace codeNamespace,
     CodeTypeDeclaration testClassDeclaration,
     CodeTypeMember typeMember,
     BuildDataDictionary buildData,
     ISetupAndTearDownContext setUpTearDownContext,
     string baseKey)
     : base(codeNamespace,
         testClassDeclaration,
         typeMember,
         buildData,
         setUpTearDownContext,
         baseKey)
 {
 }
Пример #31
0
 /// <devdoc>
 /// <para>Inserts a <see cref='System.CodeDom.CodeTypeMember'/> into the <see cref='System.CodeDom.CodeTypeMemberCollection'/> at the specified index.</para>
 /// </devdoc>
 public void Insert(int index, CodeTypeMember value)
 {
     List.Insert(index, value);
 }
Пример #32
0
 /// <devdoc>
 ///    <para>Returns the index of a <see cref='System.CodeDom.CodeTypeMember'/> in
 ///       the <see cref='System.CodeDom.CodeTypeMemberCollection'/> .</para>
 /// </devdoc>
 public int IndexOf(CodeTypeMember value)
 {
     return(List.IndexOf(value));
 }
Пример #33
0
 /// <devdoc>
 /// <para>Gets a value indicating whether the
 ///    <see cref='System.CodeDom.CodeTypeMemberCollection'/> contains the specified <see cref='System.CodeDom.CodeTypeMember'/>.</para>
 /// </devdoc>
 public bool Contains(CodeTypeMember value)
 {
     return(List.Contains(value));
 }
Пример #34
0
 public int Add(CodeTypeMember value) => List.Add(value);
Пример #35
0
 /// <devdoc>
 ///    <para> Removes a specific <see cref='System.CodeDom.CodeTypeMember'/> from the
 ///    <see cref='System.CodeDom.CodeTypeMemberCollection'/> .</para>
 /// </devdoc>
 public void Remove(CodeTypeMember value)
 {
     List.Remove(value);
 }
Пример #36
0
 public int IndexOf(CodeTypeMember value) => List.IndexOf(value);
Пример #37
0
 public bool Contains(CodeTypeMember value) => List.Contains(value);
Пример #38
0
 /// <devdoc>
 ///    <para>Adds a <see cref='System.CodeDom.CodeTypeMember'/> with the specified value to the
 ///    <see cref='System.CodeDom.CodeTypeMemberCollection'/> .</para>
 /// </devdoc>
 public int Add(CodeTypeMember value)
 {
     return(List.Add(value));
 }