public static CodeMemberProperty CreateTypeManagerProperty() { CodeMemberProperty property = CodeDomHelper.CreateInterfaceImplProperty("TypeManager", "IXMetaData", new CodeTypeReference("ILinqToXsdTypeManager")); property.GetStatements.Add(new CodeMethodReturnStatement(CodeDomHelper.SingletonTypeManager())); return(property); }
public static CodeTypeMember CreateStaticMethod(string methodName, string typeT, string typeT1, string parameterName, string parameterType, bool useAutoTyping) { CodeMemberMethod staticMethod = new CodeMemberMethod(); staticMethod.Name = methodName; staticMethod.Attributes = MemberAttributes.Static | MemberAttributes.Public; staticMethod.ReturnType = new CodeTypeReference(typeT); staticMethod.Parameters.Add(CreateParameter(parameterName, parameterType)); CodeExpression parameterExp = new CodeVariableReferenceExpression(parameterName); if (useAutoTyping) { staticMethod.Statements.Add( new CodeMethodReturnStatement( new CodeCastExpression( staticMethod.ReturnType, CreateMethodCall( new CodeTypeReferenceExpression(Constants.XTypedServices), Constants.ToXTypedElement, CreateMethodCall(new CodeTypeReferenceExpression(Constants.XElement), methodName, parameterExp), CodeDomHelper.SingletonTypeManager())))); } else { CodeMethodInvokeExpression methodCall = CreateMethodCall(new CodeTypeReferenceExpression(Constants.XTypedServices), methodName + "<" + GetInnerType(typeT, typeT1) + ">", parameterExp); if (typeT1 != null) { methodCall.Parameters.Add(CodeDomHelper.SingletonTypeManager()); } staticMethod.Statements.Add(new CodeMethodReturnStatement(methodCall)); } return(staticMethod); }
private void AddSubstGetStatements(CodeStatementCollection getStatements) { Debug.Assert(this.propertyOrigin == SchemaOrigin.Element); CodeExpression[] substParams = new CodeExpression[this.substitutionMembers.Count + 2]; substParams[0] = CodeDomHelper.This(); substParams[1] = CodeDomHelper.SingletonTypeManager(); int i = 2; foreach (XmlSchemaElement elem in this.substitutionMembers) { int num = i; i = num + 1; substParams[num] = CodeDomHelper.XNameGetExpression(elem.QualifiedName.Name, elem.QualifiedName.Namespace); } getStatements.Add(new CodeVariableDeclarationStatement("XTypedElement", "x", CodeDomHelper.CreateMethodCall(new CodeTypeReferenceExpression("XTypedServices"), "ToSubstitutedXTypedElement", substParams))); this.CheckOccurrence(getStatements); getStatements.Add(new CodeMethodReturnStatement(new CodeCastExpression(this.ReturnType, new CodeVariableReferenceExpression("x")))); }
public static CodeTypeMember CreateStaticMethod(string methodName, string typeT, string typeT1, string parameterName, string parameterType, bool useAutoTyping) { CodeExpression[] codeExpressionArray; CodeMemberMethod staticMethod = new CodeMemberMethod() { Name = methodName, Attributes = MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public, ReturnType = new CodeTypeReference(typeT) }; staticMethod.Parameters.Add(CodeDomHelper.CreateParameter(parameterName, parameterType)); CodeExpression parameterExp = new CodeVariableReferenceExpression(parameterName); if (!useAutoTyping) { CodeTypeReferenceExpression codeTypeReferenceExpression = new CodeTypeReferenceExpression("XTypedServices"); string str = string.Concat(methodName, "<", CodeDomHelper.GetInnerType(typeT, typeT1), ">"); codeExpressionArray = new CodeExpression[] { parameterExp }; CodeMethodInvokeExpression methodCall = CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression, str, codeExpressionArray); if (typeT1 != null) { methodCall.Parameters.Add(CodeDomHelper.SingletonTypeManager()); } staticMethod.Statements.Add(new CodeMethodReturnStatement(methodCall)); } else { CodeStatementCollection statements = staticMethod.Statements; CodeTypeReference returnType = staticMethod.ReturnType; CodeTypeReferenceExpression codeTypeReferenceExpression1 = new CodeTypeReferenceExpression("XTypedServices"); codeExpressionArray = new CodeExpression[2]; CodeTypeReferenceExpression codeTypeReferenceExpression2 = new CodeTypeReferenceExpression("XElement"); CodeExpression[] codeExpressionArray1 = new CodeExpression[] { parameterExp }; codeExpressionArray[0] = CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression2, methodName, codeExpressionArray1); codeExpressionArray[1] = CodeDomHelper.SingletonTypeManager(); statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(returnType, CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression1, "ToXTypedElement", codeExpressionArray)))); } return(staticMethod); }
private CodeExpression[] GetListParameters(bool set, bool constructor) { CodeExpression[] listParameters = null; int paramCount = 2; CodeExpression typeParam = null; CodeExpression nameOrValue = null; if (set) { paramCount++; if (!constructor) { nameOrValue = CodeDomHelper.SetValue(); } else { nameOrValue = new CodeVariableReferenceExpression(this.propertyName); } } if (!this.IsSubstitutionHead) { paramCount++; if (!this.typeRef.IsSimpleType) { typeParam = CodeDomHelper.SingletonTypeManager(); } else { typeParam = this.GetSchemaDatatypeExpression(); if (this.fixedDefaultValue != null) { paramCount++; } } } else { paramCount += this.substitutionMembers.Count; typeParam = CodeDomHelper.SingletonTypeManager(); } listParameters = new CodeExpression[paramCount]; int paramIndex = 0; int num = paramIndex; paramIndex = num + 1; listParameters[num] = CodeDomHelper.This(); int num1 = paramIndex; paramIndex = num1 + 1; listParameters[num1] = typeParam; if (nameOrValue != null) { int num2 = paramIndex; paramIndex = num2 + 1; listParameters[num2] = nameOrValue; } if (!this.IsSubstitutionHead) { int num3 = paramIndex; paramIndex = num3 + 1; listParameters[num3] = this.xNameGetExpression; } else { foreach (XmlSchemaElement elem in this.substitutionMembers) { int num4 = paramIndex; paramIndex = num4 + 1; listParameters[num4] = CodeDomHelper.XNameGetExpression(elem.QualifiedName.Name, elem.QualifiedName.Namespace); } } if (this.fixedDefaultValue != null) { if (this.FixedValue == null) { int num5 = paramIndex; paramIndex = num5 + 1; listParameters[num5] = new CodeFieldReferenceExpression(null, NameGenerator.ChangeClrName(this.propertyName, NameOptions.MakeDefaultValueField)); } else { int num6 = paramIndex; paramIndex = num6 + 1; listParameters[num6] = new CodeFieldReferenceExpression(null, NameGenerator.ChangeClrName(this.propertyName, NameOptions.MakeFixedValueField)); } } return(listParameters); }
public static CodeMemberMethod CreateXRootMethod(string returnType, string methodName, string[][] paramList, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public) { CodeTypeReference xRootType = new CodeTypeReference(returnType); CodeMemberMethod staticMethod = new CodeMemberMethod(); staticMethod.Name = methodName; staticMethod.Attributes = visibility.ToMemberAttribute() | MemberAttributes.Static; staticMethod.ReturnType = xRootType; CodeExpression[] parameterExp = new CodeExpression[paramList.Length]; for (int i = 0; i < paramList.Length; i++) { string[] paramRef = paramList[i]; // index 0 is the type name and index 1 is the parameter name staticMethod.Parameters.Add(CreateParameter(paramRef[1], paramRef[0])); parameterExp[i] = new CodeVariableReferenceExpression(paramRef[1]); } CodeExpression rootExp = new CodeVariableReferenceExpression("root"); CodeExpression doc = new CodeFieldReferenceExpression(rootExp, "doc"); staticMethod.Statements.Add( //XRoot root = new XRoot; new CodeVariableDeclarationStatement(xRootType, "root", new CodeObjectCreateExpression(xRootType))); staticMethod.Statements.Add( //root.doc = XDocument.Load(xmlFile); new CodeAssignStatement( doc, CreateMethodCall(new CodeTypeReferenceExpression("XDocument"), methodName, parameterExp))); staticMethod.Statements.Add( //XTypedElement typedRoot = XTypedServices.ToXTypedElement(....) new CodeVariableDeclarationStatement( Constants.XTypedElement, "typedRoot", CreateMethodCall( new CodeTypeReferenceExpression(Constants.XTypedServices), Constants.ToXTypedElement, new CodePropertyReferenceExpression(doc, "Root"), CodeDomHelper.SingletonTypeManager()))); staticMethod.Statements.Add( //if(typedRoot == null) new CodeConditionStatement( new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression("typedRoot"), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null) ), new CodeThrowExceptionStatement( new CodeObjectCreateExpression(Constants.LinqToXsdException, new CodePrimitiveExpression("Invalid root element in xml document.")) ))); staticMethod.Statements.Add( //root.rootObject = typedRoot new CodeAssignStatement( new CodeFieldReferenceExpression(rootExp, "rootObject"), new CodeVariableReferenceExpression("typedRoot"))); staticMethod.Statements.Add( //return root; new CodeMethodReturnStatement(rootExp)); return(staticMethod); }
public static CodeMemberMethod CreateXRootMethod(string returnType, string methodName, string[][] paramList) { CodeTypeReference xRootType = new CodeTypeReference(returnType); CodeMemberMethod staticMethod = new CodeMemberMethod() { Name = methodName, Attributes = MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public, ReturnType = xRootType }; CodeExpression[] parameterExp = new CodeExpression[(int)paramList.Length]; for (int i = 0; i < (int)paramList.Length; i++) { string[] paramRef = paramList[i]; staticMethod.Parameters.Add(CodeDomHelper.CreateParameter(paramRef[1], paramRef[0])); parameterExp[i] = new CodeVariableReferenceExpression(paramRef[1]); } CodeExpression rootExp = new CodeVariableReferenceExpression("root"); CodeExpression doc = new CodeFieldReferenceExpression(rootExp, "doc"); staticMethod.Statements.Add(new CodeVariableDeclarationStatement(xRootType, "root", new CodeObjectCreateExpression(xRootType, new CodeExpression[0]))); staticMethod.Statements.Add(new CodeAssignStatement(doc, CodeDomHelper.CreateMethodCall(new CodeTypeReferenceExpression("XDocument"), methodName, parameterExp))); CodeStatementCollection statements = staticMethod.Statements; CodeTypeReferenceExpression codeTypeReferenceExpression = new CodeTypeReferenceExpression("XTypedServices"); CodeExpression[] codePropertyReferenceExpression = new CodeExpression[] { new CodePropertyReferenceExpression(doc, "Root"), CodeDomHelper.SingletonTypeManager() }; statements.Add(new CodeVariableDeclarationStatement("XTypedElement", "typedRoot", CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression, "ToXTypedElement", codePropertyReferenceExpression))); CodeStatementCollection codeStatementCollection = staticMethod.Statements; CodeBinaryOperatorExpression codeBinaryOperatorExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("typedRoot"), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)); CodeStatement[] codeThrowExceptionStatement = new CodeStatement[1]; codePropertyReferenceExpression = new CodeExpression[] { new CodePrimitiveExpression("Invalid root element in xml document.") }; codeThrowExceptionStatement[0] = new CodeThrowExceptionStatement(new CodeObjectCreateExpression("LinqToXsdException", codePropertyReferenceExpression)); codeStatementCollection.Add(new CodeConditionStatement(codeBinaryOperatorExpression, codeThrowExceptionStatement)); staticMethod.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(rootExp, "rootObject"), new CodeVariableReferenceExpression("typedRoot"))); staticMethod.Statements.Add(new CodeMethodReturnStatement(rootExp)); return(staticMethod); }