Пример #1
0
        public CodeMemberMethod GenerateFromXmlMethod()
        {
            CodeMemberMethod fromXmlMethod = new CodeMemberMethod();

            fromXmlMethod.Name = FromXmlMethodName;
            fromXmlMethod.Attributes = MemberAttributes.Final | MemberAttributes.Public|MemberAttributes.Static;

            CodeTypeParameter tTypeParameter = new CodeTypeParameter("T");
            //tType.HasConstructorConstraint = true;

            fromXmlMethod.TypeParameters.Add(tTypeParameter);
            fromXmlMethod.ReturnType = new CodeTypeReference("T");;
            //fromXmlMethod.Statements.Add();
            fromXmlMethod.Parameters.Add(new CodeParameterDeclarationExpression(
                new CodeTypeReference("String"), "xml"));
            fromXmlMethod.Comments.Add(new CodeCommentStatement("Call it using this code: YourStrongTypedEntity entity = FromXml<YourStrongTypedEntity>(YourMsgString);"));
            fromXmlMethod.Statements.Add(new CodeSnippetStatement(@"        T returnedXmlClass = default(T);

            using (TextReader reader = new StringReader(xml))
            {
            returnedXmlClass = (T)new XmlSerializer(typeof(T)).Deserialize(reader);
            }
            return returnedXmlClass ; "));

            return fromXmlMethod;
        }
Пример #2
0
 public static CodeMemberMethod Generic(this CodeMemberMethod method, string paramName,
     params Type[] constraints)
 {
     var p = new CodeTypeParameter(paramName);
     p.Constraints.AddRange(constraints.Select((t) => new CodeTypeReference(t)).ToArray());
     method.TypeParameters.Add(p);
     return method;
 }
 public void AddRange(CodeTypeParameter[] value) {
     if (value == null) {
         throw new ArgumentNullException("value");
     }
     for (int i = 0; ((i) < (value.Length)); i = ((i) + (1))) {
         this.Add(value[i]);
     }
 }
Пример #4
0
 public static CodeTypeDeclaration Generic(this CodeTypeDeclaration @class, string paramName,
     params Type[] constraints)
 {
     var p = new CodeTypeParameter(paramName);
     p.Constraints.AddRange(constraints.Select((t) => new CodeTypeReference(t)).ToArray());
     @class.TypeParameters.Add(p);
     return @class;
 }
		public void Constructor1_NullItem ()
		{
			CodeTypeParameter[] typeParams = new CodeTypeParameter[] { 
				new CodeTypeParameter (), null };

			CodeTypeParameterCollection coll = new CodeTypeParameterCollection (
				typeParams);
		}
Пример #6
0
 public CodeTypeParameter ToCodeDom()
 {
     CodeTypeParameter typeParameter = new CodeTypeParameter(this.Name);
     typeParameter.CustomAttributes.AddRange(this.CustomAttributes.ToCodeDom());
     foreach (ITypeDeclaration constraint in this.Constraints)
         typeParameter.Constraints.Add(constraint.TypeReference);
     return typeParameter;
 }
Пример #7
0
 public static CodeMemberMethod Generic(this CodeMemberMethod method, string paramName,
     bool hasConstructor, params CodeTypeReference[] constraints)
 {
     var p = new CodeTypeParameter(paramName) {HasConstructorConstraint = hasConstructor};
     p.Constraints.AddRange(constraints);
     method.TypeParameters.Add(p);
     return method;
 }
		public void AddRange (CodeTypeParameter[] value)
		{
			if (value == null) {
				throw new ArgumentNullException ("value");
			}

			for (int i = 0; i < value.Length; i++) {
				Add (value[i]);
			}
		}
Пример #9
0
		public void Constructor1_Deny_Unrestricted ()
		{
			CodeTypeParameter ctp = new CodeTypeParameter ("mono");
			Assert.AreEqual (0, ctp.Constraints.Count, "Constraints");
			Assert.AreEqual (0, ctp.CustomAttributes.Count, "CustomAttributes");
			Assert.IsFalse (ctp.HasConstructorConstraint, "HasConstructorConstraint");
			ctp.HasConstructorConstraint = true;
			Assert.AreEqual ("mono", ctp.Name);
			ctp.Name = String.Empty;
		}
        public string Evaluate(CodeTypeParameter codeTypeParameter)
        {
            var typeParameterConstraint = string.Empty;
            if (codeTypeParameter.Constraints.Count > 0)
            {
                var constraint = codeTypeParameter.Constraints.OfType<CodeTypeReference>().First();
                var type = _typescriptTypeMapper.GetTypeOutput(constraint);
                typeParameterConstraint = $" extends {type}";
            }

            return $"{codeTypeParameter.Name}{typeParameterConstraint}";
        }
		public void Constructor1 ()
		{
			CodeTypeParameter tp1 = new CodeTypeParameter ();
			CodeTypeParameter tp2 = new CodeTypeParameter ();

			CodeTypeParameter[] typeParams = new CodeTypeParameter[] { tp1, tp2 };
			CodeTypeParameterCollection coll = new CodeTypeParameterCollection (
				typeParams);

			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (tp1), "#2");
			Assert.AreEqual (1, coll.IndexOf (tp2), "#3");
		}
		public void Constructor2 ()
		{
			CodeTypeParameter tp1 = new CodeTypeParameter ();
			CodeTypeParameter tp2 = new CodeTypeParameter ();

			CodeTypeParameterCollection c = new CodeTypeParameterCollection ();
			c.Add (tp1);
			c.Add (tp2);

			CodeTypeParameterCollection coll = new CodeTypeParameterCollection (c);
			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (tp1), "#2");
			Assert.AreEqual (1, coll.IndexOf (tp2), "#3");
		}
Пример #13
0
		public void Constructor1 () {
			string parameterName = "mono";

			CodeTypeParameter ctp = new CodeTypeParameter (parameterName);

			Assert.IsNotNull (ctp.Constraints, "#1");
			Assert.AreEqual (0, ctp.Constraints.Count, "#2");
			Assert.IsNotNull (ctp.CustomAttributes, "#3");
			Assert.AreEqual (0, ctp.CustomAttributes.Count, "#4");
			Assert.IsFalse (ctp.HasConstructorConstraint, "#5");
			Assert.IsNotNull (ctp.Name, "#6");
			Assert.AreSame (parameterName, ctp.Name, "#7");

			ctp.Name = null;
			Assert.IsNotNull (ctp.Name, "#8");
			Assert.AreEqual (string.Empty, ctp.Name, "#9");

			ctp = new CodeTypeParameter ((string) null);
			Assert.IsNotNull (ctp.Name, "#10");
			Assert.AreEqual (string.Empty, ctp.Name, "#11");
		}
        public CodeMemberMethod[] GetFindAllByNameExMethods()
        {
            string methodName = "FindAllByNameEx";

            SAPAutomationHelper.Current.SetSAPApiAssembly();
            Assembly asm = SAPAutomationHelper.Current.SAPGuiApiAssembly;

            var interfaces = asm.GetTypes().Where(t => t.IsInterface && t.Name.StartsWith("Gui") && t.GetInterfaces()[0].GetMethod(methodName) != null).ToArray();

            CodeMemberMethod[] methods = new CodeMemberMethod[interfaces.Count()];

            for (int i = 0; i < interfaces.Count(); i++)
            {

                methods[i] = new CodeMemberMethod();
                methods[i].Name = methodName;
                methods[i].Attributes = MemberAttributes.Public | MemberAttributes.Static;
                CodeParameterDeclarationExpression para1 = new CodeParameterDeclarationExpression(new CodeTypeReference("this " + interfaces[i].Name), interfaces[i].Name.Substring(3));
                methods[i].Parameters.Add(para1);
                CodeParameterDeclarationExpression para2 = new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "Name");
                methods[i].Parameters.Add(para2);
                CodeParameterDeclarationExpression para3 = new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(int)), "TypeId");
                methods[i].Parameters.Add(para3);
                methods[i].ReturnType = new CodeTypeReference("IEnumerable<T>");
                CodeTypeParameter param = new CodeTypeParameter("T");
                param.Constraints.Add(" class");
                methods[i].TypeParameters.Add(param);

                methods[i].Statements.Add(new CodeMethodReturnStatement(
                        new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression(null, "findAllByNameExTemplate", new CodeTypeReference("T")),
                            new CodeVariableReferenceExpression("Name"),
                            new CodeVariableReferenceExpression("TypeId"),
                            new CodeArgumentReferenceExpression(interfaces[i].Name.Substring(3) + "." + methodName)

                    )));
            }
            return methods;
        }
Пример #15
0
		public void Constructor0 ()
		{
			CodeTypeParameter ctp = new CodeTypeParameter ();

			Assert.IsNotNull (ctp.Constraints, "#1");
			Assert.AreEqual (0, ctp.Constraints.Count, "#2");
			Assert.IsNotNull (ctp.CustomAttributes, "#3");
			Assert.AreEqual (0, ctp.CustomAttributes.Count, "#4");
			Assert.IsFalse (ctp.HasConstructorConstraint, "#5");
			Assert.IsNotNull (ctp.Name, "#6");
			Assert.AreEqual (string.Empty, ctp.Name, "#7");

			ctp.Name = null;
			Assert.IsNotNull (ctp.Name, "#8");
			Assert.AreEqual (string.Empty, ctp.Name, "#9");

			string name = "mono";
			ctp.Name = name;
			Assert.AreSame (name, ctp.Name, "#10");

			ctp.HasConstructorConstraint = true;
			Assert.IsTrue (ctp.HasConstructorConstraint, "#11");
		}
 public void Remove(CodeTypeParameter value)
 {
     throw new NotImplementedException();
 }
		public void Remove ()
		{
			CodeTypeParameter ctp1 = new CodeTypeParameter ();
			CodeTypeParameter ctp2 = new CodeTypeParameter ();

			CodeTypeParameterCollection coll = new CodeTypeParameterCollection ();
			coll.Add (ctp1);
			coll.Add (ctp2);
			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (ctp1), "#2");
			Assert.AreEqual (1, coll.IndexOf (ctp2), "#3");
			coll.Remove (ctp1);
			Assert.AreEqual (1, coll.Count, "#4");
			Assert.AreEqual (-1, coll.IndexOf (ctp1), "#5");
			Assert.AreEqual (0, coll.IndexOf (ctp2), "#6");
		}
 public int IndexOf(CodeTypeParameter value)
 {
     throw new NotImplementedException();
 }
Пример #19
0
        // Create a CodeDOM graph.
        static void CreateGraph(CodeDomProvider provider, CodeCompileUnit cu)
        {
            //<Snippet8>
            if (!provider.Supports(GeneratorSupport.GenericTypeReference |
                                   GeneratorSupport.GenericTypeDeclaration))
            {
                // Return if the generator does not support generics.
                return;
            }
            //</Snippet8>

            CodeNamespace ns = new CodeNamespace("DemoNamespace");

            ns.Imports.Add(new CodeNamespaceImport("System"));
            ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            cu.Namespaces.Add(ns);

            // Declare a generic class.
            CodeTypeDeclaration class1 = new CodeTypeDeclaration();

            class1.Name = "MyDictionary";
            class1.BaseTypes.Add(new CodeTypeReference("Dictionary",
                                                       new CodeTypeReference[] {
                new CodeTypeReference("TKey"),
                new CodeTypeReference("TValue"),
            }));
            //<Snippet2>
            //<Snippet10>
            CodeTypeParameter kType = new CodeTypeParameter("TKey");

            //</Snippet2>
            //<Snippet3>
            kType.HasConstructorConstraint = true;
            //</Snippet3>
            //<Snippet4>
            kType.Constraints.Add(new CodeTypeReference(typeof(IComparable)));
            //</Snippet4>
            //<Snippet5>
            kType.CustomAttributes.Add(new CodeAttributeDeclaration(
                                           "System.ComponentModel.DescriptionAttribute",
                                           new CodeAttributeArgument(new CodePrimitiveExpression("KeyType"))));
            //</Snippet5>

            CodeTypeReference iComparableT = new CodeTypeReference("IComparable");

            iComparableT.TypeArguments.Add(new CodeTypeReference(kType));

            kType.Constraints.Add(iComparableT);

            CodeTypeParameter vType = new CodeTypeParameter("TValue");

            vType.Constraints.Add(new CodeTypeReference(typeof(IList <System.String>)));
            vType.CustomAttributes.Add(new CodeAttributeDeclaration(
                                           "System.ComponentModel.DescriptionAttribute",
                                           new CodeAttributeArgument(new CodePrimitiveExpression("ValueType"))));

            class1.TypeParameters.Add(kType);
            class1.TypeParameters.Add(vType);
            //</Snippet10>

            ns.Types.Add(class1);

            //<Snippet6>
            // Declare a generic method.
            CodeMemberMethod  printMethod = new CodeMemberMethod();
            CodeTypeParameter sType       = new CodeTypeParameter("S");

            sType.HasConstructorConstraint = true;
            CodeTypeParameter tType = new CodeTypeParameter("T");

            sType.HasConstructorConstraint = true;

            printMethod.Name = "Print";
            printMethod.TypeParameters.Add(sType);
            printMethod.TypeParameters.Add(tType);

            //</Snippet6>
            //<Snippet7>
            printMethod.Statements.Add(ConsoleWriteLineStatement(
                                           new CodeDefaultValueExpression(new CodeTypeReference("T"))));
            printMethod.Statements.Add(ConsoleWriteLineStatement(
                                           new CodeDefaultValueExpression(new CodeTypeReference("S"))));
            //</Snippet7>

            printMethod.Attributes = MemberAttributes.Public;
            class1.Members.Add(printMethod);

            CodeTypeDeclaration class2 = new CodeTypeDeclaration();

            class2.Name = "Demo";

            CodeEntryPointMethod methodMain = new CodeEntryPointMethod();

            CodeTypeReference myClass = new CodeTypeReference(
                "MyDictionary",
                new CodeTypeReference[] {
                new CodeTypeReference(typeof(int)),
                new CodeTypeReference("List",
                                      new CodeTypeReference[]
                                      { new CodeTypeReference("System.String") })
            });

            methodMain.Statements.Add(
                new CodeVariableDeclarationStatement(myClass,
                                                     "dict",
                                                     new CodeObjectCreateExpression(myClass)));

            methodMain.Statements.Add(ConsoleWriteLineStatement(
                                          new CodePropertyReferenceExpression(
                                              new CodeVariableReferenceExpression("dict"),
                                              "Count")));

//<Snippet9>
            methodMain.Statements.Add(new CodeExpressionStatement(
                                          new CodeMethodInvokeExpression(
                                              new CodeMethodReferenceExpression(
                                                  new CodeVariableReferenceExpression("dict"),
                                                  "Print",
                                                  new CodeTypeReference[] {
                new CodeTypeReference("System.Decimal"),
                new CodeTypeReference("System.Int32"),
            }),
                                              new CodeExpression[0])));

//</Snippet9>
            string dictionaryTypeName = typeof(System.Collections.Generic.Dictionary <int,
                                                                                      System.Collections.Generic.List <string> >[]).FullName;

            CodeTypeReference dictionaryType = new CodeTypeReference(dictionaryTypeName);

            methodMain.Statements.Add(
                new CodeVariableDeclarationStatement(dictionaryType, "dict2",
                                                     new CodeArrayCreateExpression(dictionaryType, new CodeExpression[1] {
                new CodePrimitiveExpression(null)
            })));

            methodMain.Statements.Add(ConsoleWriteLineStatement(
                                          new CodePropertyReferenceExpression(
                                              new CodeVariableReferenceExpression("dict2"),
                                              "Length")));

            class2.Members.Add(methodMain);
            ns.Types.Add(class2);
        }
Пример #20
0
 public CodeTypeReference(CodeTypeParameter typeParameter)
 {
     throw new NotImplementedException();
 }
Пример #21
0
		void OutputTypeParameterConstraints (CodeTypeParameter parameter)
		{
			int constraint_count = parameter.Constraints.Count +
				(parameter.HasConstructorConstraint ? 1 : 0);

			if (constraint_count == 0)
				return;

			Output.Write (" As ");

			if (constraint_count > 1)
				Output.Write (" {");

			for (int i = 0; i < parameter.Constraints.Count; i++) {
				if (i > 0)
					Output.Write (", ");
				OutputType (parameter.Constraints [i]);
			}

			if (parameter.HasConstructorConstraint) {
				if (constraint_count > 1)
					Output.Write (", ");
				Output.Write ("New");
			}

			if (constraint_count > 1)
				Output.Write ("}");
		}
 public int Add(CodeTypeParameter value) => List.Add(value);
Пример #23
0
        public static CodeTypeParameterCollection GenericTypeParameters(Type t)
        {
            if (!t.IsGenericType) return null; 

            var p = new CodeTypeParameterCollection();
            foreach (var genericParameter in t.GetGenericTypeDefinition().GetGenericArguments())
            {
                var param = new CodeTypeParameter(genericParameter.Name);
                if ((genericParameter.GenericParameterAttributes &
                     GenericParameterAttributes.ReferenceTypeConstraint) != GenericParameterAttributes.None)
                {
                    param.Constraints.Add(" class");
                }
                if ((genericParameter.GenericParameterAttributes &
                     GenericParameterAttributes.NotNullableValueTypeConstraint) != GenericParameterAttributes.None)
                {
                    param.Constraints.Add(" struct");
                }
                var constraints = genericParameter.GetGenericParameterConstraints();
                foreach (var constraintType in constraints)
                {
                    param.Constraints.Add(
                        new CodeTypeReference(TypeUtils.GetParameterizedTemplateName(constraintType, false,
                            x => true)));
                }
                if ((genericParameter.GenericParameterAttributes &
                     GenericParameterAttributes.DefaultConstructorConstraint) != GenericParameterAttributes.None)
                {
                    param.HasConstructorConstraint = true;
                }
                p.Add(param);
            }
            return p;
        }
 public void Remove(CodeTypeParameter value)
 {
 }
 public bool Contains(CodeTypeParameter value)
 {
     return(default(bool));
 }
 public bool Contains(CodeTypeParameter value) => List.Contains(value);
 public CodeTypeReference(CodeTypeParameter typeParameter)
 {
 }
 public void Insert(int index, CodeTypeParameter value)
 {
     throw new NotImplementedException();
 }
Пример #29
0
 public bool Contains(CodeTypeParameter value)
 {
     return(List.Contains(value));
 }
        private static void PopulateGenericParameters(IGenericParameterProvider publicType, CodeTypeParameterCollection parameters)
        {
            foreach (var parameter in publicType.GenericParameters)
            {
                if (parameter.HasCustomAttributes)
                    throw new NotImplementedException("Attributes on type parameters is not supported. And weird");

                // A little hacky. Means we get "in" and "out" prefixed on any constraints, but it's either that
                // or add it as a custom attribute, which looks even weirder
                var name = parameter.Name;
                if (parameter.IsCovariant)
                    name = "out " + name;
                if (parameter.IsContravariant)
                    name = "in " + name;

                var typeParameter = new CodeTypeParameter(name)
                {
                    HasConstructorConstraint =
                        parameter.HasDefaultConstructorConstraint && !parameter.HasNotNullableValueTypeConstraint
                };
                if (parameter.HasNotNullableValueTypeConstraint)
                    typeParameter.Constraints.Add(" struct"); // Extra space is a hack!
                if (parameter.HasReferenceTypeConstraint)
                    typeParameter.Constraints.Add(" class");
                foreach (var constraint in parameter.Constraints.Where(t => t.FullName != "System.ValueType"))
                {
                    typeParameter.Constraints.Add(CreateCodeTypeReference(constraint.GetElementType()));
                }
                parameters.Add(typeParameter);
            }
        }
Пример #31
0
 public void Insert(int index, CodeTypeParameter value)
 {
     List.Insert(index, value);
 }
Пример #32
0
 public CodeTypeReference(CodeTypeParameter typeParameter) :
     this((typeParameter == null) ? (string)null : typeParameter.Name)
 {
     _referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
 }
 public CodeTypeReference(CodeTypeParameter typeParameter)
 {
 }
 public int Add(CodeTypeParameter value)
 {
     return(default(int));
 }
 public int IndexOf(CodeTypeParameter value) => List.IndexOf(value);
 public int IndexOf(CodeTypeParameter value)
 {
     return(default(int));
 }
Пример #37
0
		void VisitCodeTypeParameter(CodeTypeParameter parameter)
		{
			WriteLine("VisitCodeTypeParameter: " + parameter.Name);
		}
Пример #38
0
 private void OutputTypeParameterConstraints(CodeTypeParameter typeParameter)
 {
     CodeTypeReferenceCollection constraints = typeParameter.Constraints;
     int count = constraints.Count;
     if (typeParameter.HasConstructorConstraint)
     {
         count++;
     }
     if (count != 0)
     {
         base.Output.Write(" As ");
         if (count > 1)
         {
             base.Output.Write(" {");
         }
         bool flag = true;
         foreach (CodeTypeReference reference in constraints)
         {
             if (flag)
             {
                 flag = false;
             }
             else
             {
                 base.Output.Write(", ");
             }
             base.Output.Write(this.GetTypeOutput(reference));
         }
         if (typeParameter.HasConstructorConstraint)
         {
             if (!flag)
             {
                 base.Output.Write(", ");
             }
             base.Output.Write("New");
         }
         if (count > 1)
         {
             base.Output.Write('}');
         }
     }
 }
Пример #39
0
 public CodeTypeReference(CodeTypeParameter typeParameter): 
         this( (typeParameter == null) ? (string)null : typeParameter.Name) {  
     referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter; 
 }
Пример #40
0
 public CodeTypeReference(CodeTypeParameter typeParameter) :
     this(typeParameter.Name)
 {
     this.referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
 }
Пример #41
0
 static void ConvertRequestToGeneric(SdkMessagePair messagePair, CodeTypeDeclaration requestClass, CodeMemberProperty requestField)
 {
     var parameter = new CodeTypeParameter(requestField.Type.BaseType)
         {
             HasConstructorConstraint = true
         };
     parameter.Constraints.Add(new CodeTypeReference(EntityClassBaseType));
     requestClass.TypeParameters.Add(parameter);
     requestClass.Members.Add(Constructor(new CodeExpression[] {New(requestField.Type, new CodeExpression[0])}));
     var constructor = Constructor(Param(requestField.Type, "target"), new CodeStatement[] {AssignProp(requestField.Name, VarRef("target"))});
     constructor.Statements.Add(AssignProp(RequestNamePropertyName, new CodePrimitiveExpression(messagePair.Request.Name)));
     requestClass.Members.Add(constructor);
 }
Пример #42
0
		public void Constructor4_Deny_Unrestricted ()
		{
			CodeTypeParameter parameter = new CodeTypeParameter ("System.Int32");
			CodeTypeReference ctr = new CodeTypeReference (parameter);
			Assert.IsNull (ctr.ArrayElementType, "ArrayElementType");
			Assert.AreEqual ("System.Int32", ctr.BaseType, "BaseType");
			Assert.AreEqual (0, ctr.ArrayRank, "ArrayRank");
			ctr.ArrayElementType = new CodeTypeReference ();
			ctr.BaseType = String.Empty;
			ctr.ArrayRank = 1;
			Assert.AreEqual (CodeTypeReferenceOptions.GenericTypeParameter, ctr.Options, "Options");
			ctr.Options = CodeTypeReferenceOptions.GlobalReference;
			Assert.AreEqual (0, ctr.TypeArguments.Count, "TypeArguments");
		}
        private void AddCreateNewElementMethod(CodeTypeDeclaration declaration)
        {
            CodeMemberMethod method = new CodeMemberMethod();
            method.Name = "CreateNewElement";

            CodeTypeParameter genericParameter = new CodeTypeParameter("T");
            genericParameter.HasConstructorConstraint = false;

            method.TypeParameters.Add(genericParameter);

            method.ReturnType = new CodeTypeReference(genericParameter);
            method.ImplementationTypes.Add(typeof(IXmlDataProviderHelper));

            method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IData), "data"));
            CodeParameterDeclarationExpression parameter = new CodeParameterDeclarationExpression(typeof(XElement), "newElement");
            parameter.Direction = FieldDirection.Out;
            method.Parameters.Add(parameter);
            method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "elementName"));
            method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "providerName"));

            method.Attributes = MemberAttributes.Public | MemberAttributes.Override;

            method.Statements.Add(new CodeVariableDeclarationStatement(
                InterfaceName,
                "xmlData"));

            method.Statements.Add(new CodeTryCatchFinallyStatement(
                    new CodeStatement[] {
                            new CodeAssignStatement(
                                new CodeVariableReferenceExpression("xmlData"),
                                new CodeCastExpression(
                                    InterfaceName,
                                    new CodeVariableReferenceExpression("data"))
                            )
                    },
                    new CodeCatchClause[] {
                         new CodeCatchClause("e", new CodeTypeReference(typeof(Exception)), new CodeStatement[] {
                            new CodeThrowExceptionStatement(
                                new CodeObjectCreateExpression(
                                    new CodeTypeReference(typeof(ArgumentException)),
                                    new CodeExpression[] {
                                        new CodeMethodInvokeExpression(
                                            new CodeTypeReferenceExpression(typeof(string)),
                                            "Format",
                                            new CodeExpression[] {
                                                new CodePrimitiveExpression("The type ({0}) of the given data parameter does not match the type {1}"),
                                                new CodeMethodInvokeExpression(
                                                    new CodeVariableReferenceExpression("data"),
                                                    "GetType",
                                                    new CodeExpression[] {}
                                                ),
                                                new CodeTypeOfExpression(InterfaceName)
                                            }
                                        ),
                                        new CodeVariableReferenceExpression("e")
                                    }
                                )
                            )
                        })
                    }
                ));

            const string newElementVariableName = "newElement";

            method.Statements.Add(new CodeAssignStatement(
                new CodeVariableReferenceExpression(newElementVariableName),
                new CodeObjectCreateExpression(
                        typeof(XElement),
                        new CodeVariableReferenceExpression("elementName")
                    )
            ));

            foreach (DataFieldDescriptor field in _dataTypeDescriptor.Fields)
            {
                method.Statements.Add(new CodeCommentStatement(string.Format("Interface Property {0}", field.Name)));

                CodeMethodInvokeExpression codeExpression = new CodeMethodInvokeExpression(
                            new CodeVariableReferenceExpression(newElementVariableName),
                            "Add",
                            new CodeExpression[] {
                                    new CodeObjectCreateExpression(
                                        typeof(XAttribute),
                                        new CodeExpression[] {
                                            new CodePrimitiveExpression(field.Name),
                                            new CodePropertyReferenceExpression(
                                                new CodeVariableReferenceExpression("xmlData"),
                                                field.Name
                                            )
                                        }
                                    )
                                }
                        );

                if ((field.InstanceType.IsGenericType) &&
                    (field.InstanceType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {
                    method.Statements.Add(
                        new CodeConditionStatement(
                            new CodeBinaryOperatorExpression(
                                new CodePropertyReferenceExpression(
                                    new CodePropertyReferenceExpression(
                                        new CodeVariableReferenceExpression("xmlData"),
                                        field.Name
                                    ),
                                    "HasValue"
                                ),
                                CodeBinaryOperatorType.IdentityEquality,
                                new CodePrimitiveExpression(true)
                            ),
                            new CodeStatement[] {
                                    new CodeExpressionStatement(
                                        codeExpression
                                    )
                                }
                        ));
                }
                else if (field.InstanceType == typeof(string))
                {
                    method.Statements.Add(
                        new CodeConditionStatement(
                            new CodeBinaryOperatorExpression(
                                new CodePropertyReferenceExpression(
                                    new CodeVariableReferenceExpression("xmlData"),
                                    field.Name
                                ),
                                CodeBinaryOperatorType.IdentityInequality,
                                new CodePrimitiveExpression(null)
                            ),
                            new CodeStatement[] {
                                    new CodeExpressionStatement(
                                        codeExpression
                                    )
                                }
                        ));
                }
                else
                {
                    method.Statements.Add(codeExpression);
                }
            }

            method.Statements.Add(new CodeCommentStatement("Done with properties"));

            method.Statements.Add(new CodeVariableDeclarationStatement(
                    typeof(IData),
                    "newData",
                    new CodeObjectCreateExpression(
                        _wrapperClassName,
                        new CodeExpression[] {
                            new CodeVariableReferenceExpression("newElement"),
                            new CodeObjectCreateExpression(
                                typeof(DataSourceId),
                                new CodeExpression[] {
                                    new CodeObjectCreateExpression(
                                        _dataIdClassName,
                                        new CodeVariableReferenceExpression("newElement")
                                    ),
                                    new CodeVariableReferenceExpression("providerName"),
                                    new CodeTypeOfExpression(InterfaceName)
                                }
                            )
                        }
                    )
                ));

            method.Statements.Add(new CodeMethodReturnStatement(
                    new CodeCastExpression(
                        new CodeTypeReference(genericParameter),
                        new CodeVariableReferenceExpression("newData")
                    )
                ));

            declaration.Members.Add(method);
        }
Пример #44
0
 public int Add(CodeTypeParameter value)
 {
     return(List.Add(value));
 }
Пример #45
0
 private void ValidateTypeParameter(CodeTypeParameter e) {
     ValidateIdentifier(e, "Name", e.Name);
     ValidateTypeReferences(e.Constraints);
     ValidateAttributes(e.CustomAttributes);
 }
Пример #46
0
 public int IndexOf(CodeTypeParameter value)
 {
     return(List.IndexOf(value));
 }
		public void Insert ()
		{
			CodeTypeParameter tp1 = new CodeTypeParameter ();
			CodeTypeParameter tp2 = new CodeTypeParameter ();

			CodeTypeParameterCollection coll = new CodeTypeParameterCollection ();
			coll.Add (tp1);
			Assert.AreEqual (1, coll.Count, "#1");
			Assert.AreEqual (0, coll.IndexOf (tp1), "#2");
			coll.Insert (0, tp2);
			Assert.AreEqual (2, coll.Count, "#3");
			Assert.AreEqual (1, coll.IndexOf (tp1), "#4");
			Assert.AreEqual (0, coll.IndexOf (tp2), "#5");
		}
Пример #48
0
 public void Remove(CodeTypeParameter value)
 {
     List.Remove(value);
 }
		public void AddRange ()
		{
			CodeTypeParameter tp1 = new CodeTypeParameter ();
			CodeTypeParameter tp2 = new CodeTypeParameter ();
			CodeTypeParameter tp3 = new CodeTypeParameter ();

			CodeTypeParameterCollection coll1 = new CodeTypeParameterCollection ();
			coll1.Add (tp1);
			coll1.Add (tp2);

			CodeTypeParameterCollection coll2 = new CodeTypeParameterCollection ();
			coll2.Add (tp3);
			coll2.AddRange (coll1);
			Assert.AreEqual (3, coll2.Count, "#1");
			Assert.AreEqual (1, coll2.IndexOf (tp1), "#2");
			Assert.AreEqual (2, coll2.IndexOf (tp2), "#3");
			Assert.AreEqual (0, coll2.IndexOf (tp3), "#4");

			CodeTypeParameterCollection coll3 = new CodeTypeParameterCollection ();
			coll3.Add (tp3);
			coll3.AddRange (new CodeTypeParameter[] { tp1, tp2 });
			Assert.AreEqual (3, coll2.Count, "#5");
			Assert.AreEqual (1, coll2.IndexOf (tp1), "#6");
			Assert.AreEqual (2, coll2.IndexOf (tp2), "#7");
			Assert.AreEqual (0, coll2.IndexOf (tp3), "#8");
		}
Пример #50
0
		public CodeTypeReference( CodeTypeParameter typeParameter ) :
			this (typeParameter.Name)
		{
			this.referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
		}
 public bool Contains(CodeTypeParameter value)
 {
     throw new NotImplementedException();
 }