private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); List <CodeTypeReference> implTypes = new List <CodeTypeReference>(); if (_property.ImplementationTypes != null) { var arr = new CodeTypeReference[_property.ImplementationTypes.Count]; _property.ImplementationTypes.CopyTo(arr, 0); _property.ImplementationTypes.Clear(); _property.PrivateImplementationType = null; implTypes.AddRange(arr); } provider.GenerateCodeFromMember(_property, sw, opts); foreach (CodeTypeReference tr in implTypes) { _property.ImplementationTypes.Add(tr); } //StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); //string line = sr.ReadLine(); //while (string.IsNullOrEmpty(line) || line.StartsWith("/") || line.StartsWith("[")) // line = sr.ReadLine(); StringBuilder sb = new StringBuilder(); if (InterfaceProperties != null) { foreach (CodeTypeReference tr in implTypes) { string prop; if (InterfaceProperties.TryGetValue(tr, out prop)) { var newProp = Define.Property(_property.Type, MemberAttributes.Private, prop).Implements(tr); if (_property.HasGet) { newProp.GetStatements.Add(Emit.@return(() => [email protected](_property.Name))); newProp.HasGet = true; } if (_property.HasSet) { newProp.SetStatements.Add(Emit.assignProperty(_property.Name, () => CodeDom.VarRef("value"))); newProp.HasSet = true; } StringWriter swNew = new StringWriter(); provider.GenerateCodeFromMember(CodeDomTreeProcessor.ProcessMember(newProp, CodeDomGenerator.Language.CSharp), swNew, opts); sb.Append(swNew.ToString()); } } if (sb.Length > 0) { sb.Insert(0, Environment.NewLine); } } Text = sw.GetStringBuilder().ToString() + sb.ToString(); } }
private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); provider.GenerateCodeFromMember(_method, sw, opts); StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); string line = sr.ReadLine(); while (string.IsNullOrEmpty(line)) line = sr.ReadLine(); int idx = line.IndexOf(" " + _method.Name + "("); idx = line.LastIndexOf(' ', idx - 1); if (_method.Statements.Count > 0) { Text = "partial" + line.Remove(0, idx); Text = sw.GetStringBuilder().Replace(line, Text).ToString(); } else { line = "partial" + line.Remove(0, idx); idx = line.LastIndexOf(')'); Text = line.Remove(idx + 1) + ";" + Environment.NewLine; } } }
private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); provider.GenerateCodeFromMember(_property, sw, opts); StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); string line = sr.ReadLine(); while (string.IsNullOrEmpty(line)) { line = sr.ReadLine(); } int idx = line.IndexOf(" " + _property.Name); StringBuilder sb = new StringBuilder(); sb.Append(" this["); foreach (CodeParameterDeclarationExpression parameter in Parameters) { provider.GenerateCodeFromExpression(parameter, new StringWriter(sb), opts); sb.Append(", "); } sb.Length -= 2; sb.Append("]"); Text = sw.GetStringBuilder().Replace(line, line.Substring(0, idx) + sb.ToString() + line.Substring(idx + _property.Name.Length + 1)).ToString(); } }
private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); provider.GenerateCodeFromMember(_method, sw, opts); StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); string line = sr.ReadLine(); while (string.IsNullOrEmpty(line)) { line = sr.ReadLine(); } int idx = line.IndexOf(" " + _method.Name + "("); idx = line.LastIndexOf(' ', idx - 1); if (_method.Statements.Count > 0) { Text = "partial" + line.Remove(0, idx); Text = sw.GetStringBuilder().Replace(line, Text).ToString(); } else { line = "partial" + line.Remove(0, idx); idx = line.LastIndexOf(')'); Text = line.Remove(idx + 1) + ";" + Environment.NewLine; } } }
public override string ToString() { var w = new StringWriter(); var o = new CodeGeneratorOptions(); var provider = new Microsoft.CSharp.CSharpCodeProvider(); provider.GenerateCodeFromMember(this, w, o); return(w.ToString()); }
public static string ConvertCodeDomToPInvokeCodeImpl(LanguageType type, CodeTypeDeclarationCollection col, ErrorProvider ep) { ThrowIfNull(col); ThrowIfNull(ep); StringWriter writer = new StringWriter(); CodeDomProvider provider = default(CodeDomProvider); string commentStart = null; // Generate based on the language switch (type) { case Transform.LanguageType.VisualBasic: commentStart = "'"; provider = new Microsoft.VisualBasic.VBCodeProvider(); break; case Transform.LanguageType.CSharp: commentStart = "//"; provider = new Microsoft.CSharp.CSharpCodeProvider(); break; default: ThrowInvalidEnumValue(type); return(string.Empty); } foreach (string warning in ep.Warnings) { writer.WriteLine("{0} Warning: {1}", commentStart, warning); } foreach (string err in ep.Errors) { writer.WriteLine("{0} Error: {1}", commentStart, err); } foreach (CodeTypeDeclaration ctd in col) { provider.GenerateCodeFromMember(ctd, writer, new CodeGeneratorOptions()); } if (type == Transform.LanguageType.CSharp) { // CSharp specific fixup return(FixupCSharpCode(writer.ToString())); } else { return(writer.ToString()); } }
private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringBuilder sb = new StringBuilder(); using (IndentedTextWriter tw = new IndentedTextWriter(new StringWriter(sb), opts.IndentString)) { provider.GenerateCodeFromMember(Field, tw, opts); var type = new StringWriter(); provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(Field.Type), type, opts); sb.Replace(" " + type.GetStringBuilder(), " readonly " + type.GetStringBuilder()); } Text = sb.ToString(); } }
private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringBuilder sb = new StringBuilder(); using (IndentedTextWriter tw = new IndentedTextWriter(new StringWriter(sb), opts.IndentString)) { provider.GenerateCodeFromMember(Field, tw, opts); var type = new StringWriter(); provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(Field.Type), type, opts); sb.Replace(" " + type.GetStringBuilder(), " readonly " + type.GetStringBuilder()); } Text = sb.ToString(); } }
internal static void EmitCodeToAddIntoCustomStdBinding(Type standardBindingType, string generatedElementClassName, string generatedCollectionElementClassName, string srcFile) { CodeMemberMethod applyCfgMethodForStdBinding = new CodeMemberMethod(); applyCfgMethodForStdBinding.Name = MethodNameConstants.ApplyConfigurationMethod; string paramConfigName = "configurationName"; CodeVariableReferenceExpression paramVarRef = new CodeVariableReferenceExpression(paramConfigName); applyCfgMethodForStdBinding.Parameters.Add(new CodeParameterDeclarationExpression( CodeDomHelperObjects.stringTypeRef, paramConfigName)); string bindingsString = "bindings"; CodeVariableReferenceExpression bindingsVarRef = new CodeVariableReferenceExpression(bindingsString); string sectionString = "section"; CodeVariableReferenceExpression sectionVarRef = new CodeVariableReferenceExpression(sectionString); string elementString = "element"; CodeVariableReferenceExpression elementVarRef = new CodeVariableReferenceExpression(elementString); string topLevelSectionNameInConfig = "system.serviceModel/bindings/"; string subSectionNameInConfig = Helpers.TurnFirstCharLower(standardBindingType.Name); CodeVariableDeclarationStatement bindingsInit = new CodeVariableDeclarationStatement( new CodeTypeReference(TypeNameConstants.BindingsSection), bindingsString, new CodeCastExpression(TypeNameConstants.BindingsSection, new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(TypeNameConstants.ConfigurationManager), MethodNameConstants.GetSectionMethod, new CodePrimitiveExpression(topLevelSectionNameInConfig)))); applyCfgMethodForStdBinding.Statements.Add(bindingsInit); CodeVariableDeclarationStatement sectionInit = new CodeVariableDeclarationStatement( new CodeTypeReference(generatedCollectionElementClassName), sectionString, new CodeCastExpression(generatedCollectionElementClassName, new CodeArrayIndexerExpression( bindingsVarRef, new CodePrimitiveExpression(subSectionNameInConfig)))); applyCfgMethodForStdBinding.Statements.Add(sectionInit); CodeVariableDeclarationStatement elementInit = new CodeVariableDeclarationStatement( new CodeTypeReference(generatedElementClassName), elementString, new CodeArrayIndexerExpression( new CodeFieldReferenceExpression( sectionVarRef, PropertyNameConstants.BindingsProperty), paramVarRef)); applyCfgMethodForStdBinding.Statements.Add(elementInit); CodeBinaryOperatorExpression cboe = new CodeBinaryOperatorExpression( elementVarRef, CodeBinaryOperatorType.IdentityEquality, CodeDomHelperObjects.NullRef); CodeThrowExceptionStatement ctes = new CodeThrowExceptionStatement( new CodeObjectCreateExpression( new CodeTypeReference(typeof(ConfigurationErrorsException)), new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(CodeDomHelperObjects.stringTypeRef), MethodNameConstants.FormatMethod, CodeDomHelperObjects.cultureInfoCurrent, new CodePrimitiveExpression("There is no binding named {0} at {1}."), paramVarRef, new CodePropertyReferenceExpression( sectionVarRef, PropertyNameConstants.BindingNameProperty)))); CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression( elementVarRef, MethodNameConstants.ApplyConfigurationMethod, CodeDomHelperObjects.ThisRef); CodeStatement[] trueStatements = { ctes }; CodeStatement[] falseStatements = { new CodeExpressionStatement(cmie) }; CodeConditionStatement ccs = new CodeConditionStatement(cboe, trueStatements, falseStatements); applyCfgMethodForStdBinding.Statements.Add(ccs); string indent = " "; CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); using (System.IO.StreamWriter sbSW = new System.IO.StreamWriter(srcFile, false)) { using (IndentedTextWriter sbTW = new IndentedTextWriter(sbSW, indent)) { provider.GenerateCodeFromMember( applyCfgMethodForStdBinding, sbTW, new CodeGeneratorOptions()); } } }
private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); provider.GenerateCodeFromMember(_property, sw, opts); StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); string line = sr.ReadLine(); while (string.IsNullOrEmpty(line)) line = sr.ReadLine(); int idx = line.IndexOf(" " + _property.Name); StringBuilder sb = new StringBuilder(); sb.Append(" this["); foreach (CodeParameterDeclarationExpression parameter in Parameters) { provider.GenerateCodeFromExpression(parameter, new StringWriter(sb), opts); sb.Append(", "); } sb.Length -= 2; sb.Append("]"); Text = sw.GetStringBuilder().Replace(line, line.Substring(0, idx) + sb.ToString() + line.Substring(idx+_property.Name.Length+1)).ToString(); } }
private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); List<CodeTypeReference> implTypes = new List<CodeTypeReference>(); if (_property.ImplementationTypes != null) { var arr = new CodeTypeReference[_property.ImplementationTypes.Count]; _property.ImplementationTypes.CopyTo(arr, 0); _property.ImplementationTypes.Clear(); _property.PrivateImplementationType = null; implTypes.AddRange(arr); } provider.GenerateCodeFromMember(_property, sw, opts); foreach (CodeTypeReference tr in implTypes) { _property.ImplementationTypes.Add(tr); } //StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); //string line = sr.ReadLine(); //while (string.IsNullOrEmpty(line) || line.StartsWith("/") || line.StartsWith("[")) // line = sr.ReadLine(); StringBuilder sb = new StringBuilder(); if (InterfaceProperties != null) { foreach (CodeTypeReference tr in implTypes) { string prop; if (InterfaceProperties.TryGetValue(tr, out prop)) { var newProp = Define.Property(_property.Type, MemberAttributes.Private, prop).Implements(tr); if (_property.HasGet) { newProp.GetStatements.Add(Emit.@return(()=>[email protected](_property.Name))); newProp.HasGet = true; } if (_property.HasSet) { newProp.SetStatements.Add(Emit.assignProperty(_property.Name, () => CodeDom.VarRef("value"))); newProp.HasSet = true; } StringWriter swNew = new StringWriter(); provider.GenerateCodeFromMember(CodeDomTreeProcessor.ProcessMember(newProp, CodeDomGenerator.Language.CSharp), swNew, opts); sb.Append(swNew.ToString()); } } if (sb.Length > 0) sb.Insert(0, Environment.NewLine); } Text = sw.GetStringBuilder().ToString() + sb.ToString(); } }
internal static void EmitCodeToAddIntoCustomStdBinding(Type standardBindingType, string generatedElementClassName, string generatedCollectionElementClassName, string srcFile) { CodeMemberMethod applyCfgMethodForStdBinding = new CodeMemberMethod(); applyCfgMethodForStdBinding.Name = MethodNameConstants.ApplyConfigurationMethod; string paramConfigName = "configurationName"; CodeVariableReferenceExpression paramVarRef = new CodeVariableReferenceExpression(paramConfigName); applyCfgMethodForStdBinding.Parameters.Add(new CodeParameterDeclarationExpression( CodeDomHelperObjects.stringTypeRef, paramConfigName)); string bindingsString = "bindings"; CodeVariableReferenceExpression bindingsVarRef = new CodeVariableReferenceExpression(bindingsString); string sectionString = "section"; CodeVariableReferenceExpression sectionVarRef = new CodeVariableReferenceExpression(sectionString); string elementString = "element"; CodeVariableReferenceExpression elementVarRef = new CodeVariableReferenceExpression(elementString); string topLevelSectionNameInConfig = "system.serviceModel/bindings/"; string subSectionNameInConfig = Helpers.TurnFirstCharLower(standardBindingType.Name); CodeVariableDeclarationStatement bindingsInit = new CodeVariableDeclarationStatement( new CodeTypeReference(TypeNameConstants.BindingsSection), bindingsString, new CodeCastExpression(TypeNameConstants.BindingsSection, new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(TypeNameConstants.ConfigurationManager), MethodNameConstants.GetSectionMethod, new CodePrimitiveExpression(topLevelSectionNameInConfig)))); applyCfgMethodForStdBinding.Statements.Add(bindingsInit); CodeVariableDeclarationStatement sectionInit = new CodeVariableDeclarationStatement( new CodeTypeReference(generatedCollectionElementClassName), sectionString, new CodeCastExpression(generatedCollectionElementClassName, new CodeArrayIndexerExpression( bindingsVarRef, new CodePrimitiveExpression(subSectionNameInConfig)))); applyCfgMethodForStdBinding.Statements.Add(sectionInit); CodeVariableDeclarationStatement elementInit = new CodeVariableDeclarationStatement( new CodeTypeReference(generatedElementClassName), elementString, new CodeArrayIndexerExpression( new CodeFieldReferenceExpression( sectionVarRef, PropertyNameConstants.BindingsProperty), paramVarRef)); applyCfgMethodForStdBinding.Statements.Add(elementInit); CodeBinaryOperatorExpression cboe = new CodeBinaryOperatorExpression( elementVarRef, CodeBinaryOperatorType.IdentityEquality, CodeDomHelperObjects.NullRef); CodeThrowExceptionStatement ctes = new CodeThrowExceptionStatement( new CodeObjectCreateExpression( new CodeTypeReference(typeof(ConfigurationErrorsException)), new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(CodeDomHelperObjects.stringTypeRef), MethodNameConstants.FormatMethod, CodeDomHelperObjects.cultureInfoCurrent, new CodePrimitiveExpression("There is no binding named {0} at {1}."), paramVarRef, new CodePropertyReferenceExpression( sectionVarRef, PropertyNameConstants.BindingNameProperty)))); CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression( elementVarRef, MethodNameConstants.ApplyConfigurationMethod, CodeDomHelperObjects.ThisRef); CodeStatement[] trueStatements = { ctes }; CodeStatement[] falseStatements = { new CodeExpressionStatement(cmie) }; CodeConditionStatement ccs = new CodeConditionStatement(cboe, trueStatements, falseStatements); applyCfgMethodForStdBinding.Statements.Add(ccs); string indent = " "; CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); using (System.IO.StreamWriter sbSW = new System.IO.StreamWriter(srcFile, false)) { using (IndentedTextWriter sbTW = new IndentedTextWriter(sbSW, indent)) { provider.GenerateCodeFromMember( applyCfgMethodForStdBinding, sbTW, new CodeGeneratorOptions()); } } }