public static CodeMemberMethod CreateInterfaceImplMethod(string methodName, string interfaceName, CodeTypeReference returnType, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public) { CodeMemberMethod interfaceMethod = CreateMethod(methodName, returnType, visibility.ToMemberAttribute()); interfaceMethod.PrivateImplementationType = new CodeTypeReference(interfaceName); interfaceMethod.ImplementationTypes.Add(new CodeTypeReference(interfaceName)); return(interfaceMethod); }
public static CodeTypeMember CreateStaticMethod(string methodName, string typeT, string typeT1, string parameterName, string parameterType, bool useAutoTyping, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public) { CodeMemberMethod staticMethod = new CodeMemberMethod(); staticMethod.Name = methodName; staticMethod.Attributes = MemberAttributes.Static | visibility.ToMemberAttribute(); 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); }
public static CodeConstructor CreateXRootFunctionalConstructor(string typeName, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public) { CodeConstructor constructor = CodeDomHelper.CreateConstructor(visibility.ToMemberAttribute()); constructor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeName), "root")); constructor.Statements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression(This(), "doc"), new CodeObjectCreateExpression( "XDocument", new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("root"), Constants.Untyped)))); constructor.Statements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression(This(), "rootObject"), new CodeVariableReferenceExpression("root"))); return(constructor); }
public static CodeMemberMethod CreateXRootSave(string[][] paramList, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public) { CodeMemberMethod staticMethod = new CodeMemberMethod(); staticMethod.Name = "Save"; staticMethod.Attributes = visibility.ToMemberAttribute(); 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 doc = new CodeVariableReferenceExpression("doc"); staticMethod.Statements.Add( //root.doc = XDocument.Save(...); CreateMethodCall(doc, "Save", parameterExp)); return(staticMethod); }
private void CreateXRoot(CodeNamespace codeNamespace, string rootName, List <CodeTypeDeclaration> elements, List <CodeNamespace> namespaces, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public) { LocalSymbolTable lst = new LocalSymbolTable(); CodeTypeDeclaration xroot = CodeDomHelper.CreateTypeDeclaration(rootName, null, visibility); //Create Methods CodeMemberField docField = CodeDomHelper.CreateMemberField("doc", "XDocument", false, MemberAttributes.Private); CodeMemberField rootField = CodeDomHelper.CreateMemberField("rootObject", Constants.XTypedElement, false, MemberAttributes.Private); xroot.Members.Add(docField); xroot.Members.Add(rootField); lst.Init(rootName); lst.RegisterMember("doc"); lst.RegisterMember("rootObject"); lst.RegisterMember("Load"); lst.RegisterMember("Parse"); lst.RegisterMember("Save"); lst.RegisterMember("XDocument"); lst.RegisterMember("Root"); // Constructor xroot.Members.Add(CodeDomHelper.CreateConstructor(MemberAttributes.Private)); //Load Methods xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][] { new string[] { "System.String", "xmlFile" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][] { new string[] { "System.String", "xmlFile" }, new string[] { "LoadOptions", "options" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][] { new string[] { "TextReader", "textReader" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][] { new string[] { "TextReader", "textReader" }, new string[] { "LoadOptions", "options" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][] { new string[] { "XmlReader", "xmlReader" } }, visibility)); //Parse Methods xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", new string[][] { new string[] { "System.String", "text" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", new string[][] { new string[] { "System.String", "text" }, new string[] { "LoadOptions", "options" } }, visibility)); //Save Methods xroot.Members.Add( CodeDomHelper.CreateXRootSave(new string[][] { new string[] { "System.String", "fileName" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][] { new string[] { "TextWriter", "textWriter" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][] { new string[] { "XmlWriter", "writer" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][] { new string[] { "TextWriter", "textWriter" }, new string[] { "SaveOptions", "options" } }, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][] { new string[] { "System.String", "fileName" }, new string[] { "SaveOptions", "options" } }, visibility)); CodeMemberProperty docProp = CodeDomHelper.CreateProperty("XDocument", "XDocument", docField, visibility.ToMemberAttribute(), false); xroot.Members.Add(docProp); CodeMemberProperty rootProp = CodeDomHelper.CreateProperty("Root", "XTypedElement", rootField, visibility.ToMemberAttribute(), false); xroot.Members.Add(rootProp); for (int i = 0; i < elements.Count; i++) { string typeName = elements[i].Name; string fqTypeName = (namespaces == null || namespaces[i].Name == String.Empty) ? typeName : "global::" + namespaces[i].Name + "." + typeName; xroot.Members.Add(CodeDomHelper.CreateXRootFunctionalConstructor(fqTypeName, visibility)); xroot.Members.Add(CodeDomHelper.CreateXRootGetter(typeName, fqTypeName, lst, visibility)); } codeNamespace.Types.Add(xroot); }
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 CodeTypeMember CreateSave(string paramName, string paramType, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public) { CodeMemberMethod saveMethod = new CodeMemberMethod(); saveMethod.Name = "Save"; saveMethod.Attributes = (saveMethod.Attributes & ~MemberAttributes.AccessMask) | visibility.ToMemberAttribute(); saveMethod.Parameters.Add(CreateParameter(paramName, paramType)); saveMethod.Statements.Add( CreateMethodCall( new CodeTypeReferenceExpression(Constants.XTypedServices), "Save", new CodeVariableReferenceExpression(paramName), new CodePropertyReferenceExpression(null, Constants.Untyped))); return(saveMethod); }