c# - 存储到表达式中时枚举的类型是什么

标签 c# asp.net asp.net-mvc enums expression

我正在获取表达式 x=>x.SomeAttribute

属性可以是任何类型。 我正在从表达式中获取属性类型。 对于普通的原始属性,它会给我正确的值,如 int、string...等。 但是对于枚举,它给我 MyEnum。 我想为 Enum 执行 getExpression 方法。那么要传递什么作为 TResult。

public enum TestEnum
    {
        first = 0,
        second = 1
    }

Public Class A 
    {
         public int TestInt {get;set;}
         public string TestString {get;set;}
         public TestEnum EnumDemo { get; set; }
    }



    x=>x.TestInt;
    getExpression<TSource, int>(expression)// when SomeAttribute is integer

    x=>x.TestString;
    getExpression<TSource, string>(expression) // when SomeAttribute  is string

    x=>x.EnumDemo ;
    getExpression<TSource, Enum>(expression) // when SomeAttribute is Enum, It reruns null



private Expression<Func<TSource, TResult>> getExpression<TSource, TResult>(Expression expression)
     {
          var x = expression as Expression<Func<TSource, TMember>>;       
           return x;
     }

最佳答案

intenum 的基础类型因此

getExpression<TSource, int>(expression);

会起作用。

关于c# - 存储到表达式中时枚举的类型是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776594/

相关文章:

c# - 当我关闭应用程序时,我的程序仍在任务管理器中

C#滚动条在DataGridView更新时不断重置

asp.net - 在 C# 中将 Base64 字节数组解码为图像

c# - 无法访问 asp :TextBox tag from the C# file : the name 'txtUsername' does not exist in the current context

asp.net-mvc - 如何在 View 中加载部分 View ?

c# - 如何快速有效地从 'simple' 截图中读取文本?

c# - 返回 boolean 值

c# - 如何处理空字符串?

asp.net-mvc - 我应该在我的 Web 应用程序中登录哪些信息?

asp.net-mvc - ASP.NET MVC Azure Web 角色和临时文件夹大小