Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a code generation routine to create a return statement that returns a particular enumerated type e.g.

VB
Return EvaluationResult.Include


What I have so far is...

VB.NET
Private Shared Function ToReturnStatement(ByVal valueToReturn As IdentityGroupClassification) As CodeStatement

    Dim expression As CodeExpression = Nothing
    If (valueToReturn = IdentityGroupClassification.Exclude) Then
        'EvaluationResult.Exclude

    ElseIf (valueToReturn = IdentityGroupClassification.Include) Then
        'EvaluationResult.Include

    Else
        'EvaluationResult.Unchanged

    End If
    If (expression IsNot Nothing) Then
        Return New CodeMethodReturnStatement(expression)
    Else
        Return New CodeMethodReturnStatement()
    End If


End Function


What do I need to set "expression" to in order to return an enumerated type?
Posted

1 solution

Isn't this stuff so damned awkward! You will need a CodeTypeReferenceExpression to get the enum type and a CodeFieldReferenceExpression to get a value.

For example (in C#, sorry about that)
C#
private CodeMemberMethod CreateGetEnumMethod() {
  // To create the following method
  // public System.DayOfWeek GetDay() {
  //   return System.DayOfWeek.Saturday;
  // }

  CodeMemberMethod method = new CodeMemberMethod();
  method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  method.Name = "GetDay";
  method.ReturnType = new CodeTypeReference(typeof(DayOfWeek));

  CodeTypeReferenceExpression enumType = new CodeTypeReferenceExpression(typeof(DayOfWeek));
  CodeFieldReferenceExpression enumValue = new CodeFieldReferenceExpression(enumType, "Saturday");
  method.Statements.Add(new CodeMethodReturnStatement(enumValue));
  return method;
}

Alan.
 
Share this answer
 
Comments
Duncan Edwards Jones 26-Dec-15 12:58pm    
Thank you - that helped

The VB.NET resulting code is therefore:

Private Shared Function ToReturnStatement(ByVal valueToReturn As IdentityGroupClassification) As CodeStatement

Dim expression As CodeExpression = Nothing
Dim EvaluationResultExpression As CodeExpression = New CodeTypeReferenceExpression("EvaluationResult")
If (valueToReturn = IdentityGroupClassification.Exclude) Then
'EvaluationResult.Exclude
expression = New CodeFieldReferenceExpression(EvaluationResultExpression, "Exclude")
ElseIf (valueToReturn = IdentityGroupClassification.Include) Then
'EvaluationResult.Include
expression = New CodeFieldReferenceExpression(EvaluationResultExpression, "Include")
Else
'EvaluationResult.Unchanged
expression = New CodeFieldReferenceExpression(EvaluationResultExpression, "Unchanged")
End If
If (expression IsNot Nothing) Then
Return New CodeMethodReturnStatement(expression)
Else
Return New CodeMethodReturnStatement()
End If


End Function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900