[CodeGen]如何返回枚举类型? [英] [CodeGen] How do I return an enumerated type?

查看:144
本文介绍了[CodeGen]如何返回枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个代码生成例程来创建一个返回特定枚举类型的return语句,例如



I want to create a code generation routine to create a return statement that returns a particular enumerated type e.g.

Return EvaluationResult.Include





到目前为止,我所拥有的是... 。





What I have so far is...

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?

推荐答案

这个东西不是那么该死的尴尬!您将需要CodeTypeReferenceExpression来获取枚举类型和CodeFieldReferenceExpression来获取值。



例如(在C#中,抱歉)

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)
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。


Alan.


这篇关于[CodeGen]如何返回枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆