Created
November 19, 2010 18:22
-
-
Save rippinrobr/706896 to your computer and use it in GitHub Desktop.
adding a constructor to our generated code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_constructor | |
arg_name = "repo" | |
# Create the constructor's object | |
ctor = CodeConstructor.new | |
ctor.Attributes = MemberAttributes.Public | |
ctor.Parameters.Add(CodeParameterDeclarationExpression.new("IRepository", arg_name)) | |
# Grab the references for both the parameter we just added and the field we created | |
# earlier | |
arg_ref = CodeArgumentReferenceExpression.new(arg_name) | |
repo_ref = CodeFieldReferenceExpression.new | |
repo_ref.FieldName = @repo_field_name | |
# Creating the condition statement for the if/else that will wrap our assignment statement | |
condition_statement = CodeSnippetExpression.new("#{arg_name} != null") | |
# Create the statement that will be used when the condition statement is true | |
assign_statement = CodeAssignStatement.new(repo_ref, arg_ref) | |
ctor.Statements.Add(create_if_else_statement(condition_statement, assign_statement,@arg_null_exception_stmt)) | |
@target_class.Members.Add(ctor) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment