public EntityDeclaration CreateProperty(string modifier, string returnTypeFullName, string propertyName, string getterMethodBody, string setterMethodBody) { Ensure.ArgumentNotNullOrEmpty(returnTypeFullName, "returnTypeFullName"); Ensure.ArgumentNotNullOrEmpty(propertyName, "propertyName"); var propSource = string.Format("[{0}]{1} {2} {3} {{ {4} {5} }}", typeof(MixedInMemberAttribute).GetOriginalFullNameWithGlobal(), modifier, returnTypeFullName, propertyName, getterMethodBody, setterMethodBody); var parsedProperty = CSharpParser.ParseTypeMembers(propSource).FirstOrDefault(); if (null == parsedProperty) #region Throw Exception { throw new Exception(string.Format(Strings.ExceptionParsingCodeInCodeGeneratorProxy, "Property", propSource)); } #endregion GeneratedClassSyntaxTree.AddChild(parsedProperty, Roles.TypeMemberRole); return(parsedProperty); }
public void ImplementInterface(string interfaceFullName) { if (GeneratedClassSyntaxTree.Descendants.OfType <SimpleType>() .Any(x => x.Identifier == interfaceFullName)) { return; } GeneratedClassSyntaxTree.AddChild( new SimpleType(interfaceFullName), Roles.BaseType); }
public EntityDeclaration CreateMethod(string modifier, string returnTypeFullName, string methodName, IList <KeyValuePair <string, string> > parameters, string methodBody, string constraingClause = "", bool addDebuggerStepThroughAttribute = true) { Ensure.ArgumentNotNullOrEmpty(returnTypeFullName, "returnTypeFullName"); Ensure.ArgumentNotNullOrEmpty(methodName, "methodName"); Ensure.ArgumentNotNull(parameters, "parameters"); if (ContainsMethod(methodName, parameters)) #region Throw Exception { throw new Exception(string.Format( Strings.ExceptionCreateMethodFailedBecauseClassAlreadyContainsMethodSignature, methodName, string.Join(",", parameters.Select(x => x.Key + x.Value)))); } #endregion methodBody = methodBody.IsNullOrEmpty() ? ";" : methodBody.EnsureStartsWith("{").EnsureEndsWith("}"); var debuggerStepThroughAttribute = (addDebuggerStepThroughAttribute) ? "[global::System.Diagnostics.DebuggerStepThrough]" : ""; var methodSource = string.Format("[{0}]{1} {2} {3} {4}({5}) {6} {7} ", typeof(MixedInMemberAttribute).GetOriginalFullNameWithGlobal(), debuggerStepThroughAttribute, modifier, returnTypeFullName, methodName, string.Join(",", parameters.Select(x => x.Key + " " + x.Value)), constraingClause, methodBody); var parsedMethod = CSharpParser.ParseTypeMembers(methodSource).FirstOrDefault(); if (null == parsedMethod) #region Throw Exception { throw new Exception(string.Format(Strings.ExceptionParsingCodeInCodeGeneratorProxy, "Method", methodSource)); } #endregion GeneratedClassSyntaxTree.AddChild(parsedMethod, Roles.TypeMemberRole); return(parsedMethod); }
public void AddUsingStatement(string @namespace) { if (string.IsNullOrWhiteSpace(@namespace)) { return; } if (GeneratedClassSyntaxTree .Descendants .OfType <UsingDeclaration>() .Any(x => x.GetText().Replace("using ", "").Replace(";", "").Equals(@namespace))) { return; } GeneratedClassSyntaxTree.AddChild( new UsingDeclaration(@namespace), new Role <UsingDeclaration>("UsingDeclaration")); }
public EntityDeclaration CreateDataMember(string modifiers, string dataMemberTypeFullName, string dataMemberName, string initializerExpression = "") { Ensure.ArgumentNotNull(dataMemberTypeFullName, "dataMemberTypeFullName"); Ensure.ArgumentNotNullOrEmpty(dataMemberName, "dataMemberName"); if (ContainsDataMember(dataMemberName)) #region Throw Exception { throw new Exception(string.Format( Strings.ExceptionCreateDataMemberFailedBecauseClassAlreadyContainsDataMemberName, dataMemberName)); } #endregion var initializationString = string.IsNullOrEmpty(initializerExpression) ? ";" : initializerExpression.Trim().EnsureStartsWith("=").EnsureEndsWith(";"); var parsedDataMemberSource = string.Format("{0} {1} {2} {3}", modifiers, dataMemberTypeFullName, dataMemberName, initializationString); var parsedDataMember = CSharpParser.ParseTypeMembers(parsedDataMemberSource) .FirstOrDefault() as FieldDeclaration; if (null == parsedDataMember) #region Throw Exception { throw new Exception(string.Format(Strings.ExceptionParsingCodeInCodeGeneratorProxy, "Data Member", parsedDataMemberSource)); } #endregion GeneratedClassSyntaxTree.AddChild(parsedDataMember, Roles.TypeMemberRole); return(parsedDataMember); }
public void CreateConstructor(string modifiers, IList <KeyValuePair <string, string> > parameters, string constructorInitializer, string constructorBody) { constructorInitializer = constructorInitializer ?? ""; constructorBody = (constructorBody ?? "") .EnsureStartsWith("{").EnsureEndsWith("}"); var constructorSource = string.Format("{0} {1} ({2}) {3} {4} ", modifiers, GeneratedClassSyntaxTree.Name, string.Join(",", parameters.Select(x => x.Key + " " + x.Value)), constructorInitializer, constructorBody); var dummyClassWrapper = string.Format("public class {0}{{ {1} }}", GeneratedClassSyntaxTree.Name, constructorSource); var parsedConstructor = CSharpParser.Parse(dummyClassWrapper) .Descendants .OfType <ConstructorDeclaration>() .FirstOrDefault(); if (null == parsedConstructor) #region Throw Exception { throw new Exception(string.Format(Strings.ExceptionParsingCodeInCodeGeneratorProxy, "Constructor", constructorSource)); } #endregion GeneratedClassSyntaxTree.AddChild( (ConstructorDeclaration)parsedConstructor.Clone(), Roles.TypeMemberRole); }
public ICodeGeneratorProxy AddNestedType(TypeDeclaration nestedType) { GeneratedClassSyntaxTree.AddChildTypeDeclaration(nestedType); return(new CodeGeneratorProxy(nestedType)); }