Linq 表达式找不到公共方法...:-/

发布于 2024-12-02 02:18:53 字数 1389 浏览 0 评论 0原文

我编写一个表达式来测试对象的属性(枚举)是否设置了某些标志。

下面的代码使用枚举的 HasFlag 函数测试对象的有效性是否“包含”星期一。

实际上,Call方法似乎没有找到相应的“HasFlag”...我在下面的代码中做错了什么?

using System;
using System.Linq.Expressions;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Expression exp = null;

            var myValParam = Expression.Parameter(typeof(TestHehe), "val");
            var myValTestValidityParam = Expression.Property(myValParam, "TestValidity");

            Validity myVal = Validity.Monday;

            // Gives 'True'
            Console.WriteLine(myVal.HasFlag(myVal));

            // test it
            var myConst = Expression.Constant(myVal, myVal.GetType());

            // here!!!!!!!!!!!!!!!!!!!!!!!!!!
            exp = Expression.Call(myValTestValidityParam, "HasFlag", null, myConst);
            // No method 'HasFlag' on type 'ConsoleApplication3.Validity' 
            // is compatible with the supplied arguments.

            // just to be
            Console.WriteLine(exp.ToString());
        }
    }

    public class TestHehe
    {
        public Validity TestValidity { get; set; }
    }

    [Flags]
    public enum Validity
    {
        Monday = 0,
        Tuesday = 1,
        Wednesday = 2,
        Thursday = 4,
        Friday = 8,
        Saturday = 16,
        Sunday = 32
    }
}

I write a expression that will test if a property(enum) of a object have, or have not some flags set.

The code bellow test if the validity of an object "contains" or not Monday, using the HasFlag function of an Enum.

Actually, the Call method seems do not find a corresponding "HasFlag"... What I do wrong in the bellow code?

using System;
using System.Linq.Expressions;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Expression exp = null;

            var myValParam = Expression.Parameter(typeof(TestHehe), "val");
            var myValTestValidityParam = Expression.Property(myValParam, "TestValidity");

            Validity myVal = Validity.Monday;

            // Gives 'True'
            Console.WriteLine(myVal.HasFlag(myVal));

            // test it
            var myConst = Expression.Constant(myVal, myVal.GetType());

            // here!!!!!!!!!!!!!!!!!!!!!!!!!!
            exp = Expression.Call(myValTestValidityParam, "HasFlag", null, myConst);
            // No method 'HasFlag' on type 'ConsoleApplication3.Validity' 
            // is compatible with the supplied arguments.

            // just to be
            Console.WriteLine(exp.ToString());
        }
    }

    public class TestHehe
    {
        public Validity TestValidity { get; set; }
    }

    [Flags]
    public enum Validity
    {
        Monday = 0,
        Tuesday = 1,
        Wednesday = 2,
        Thursday = 4,
        Friday = 8,
        Saturday = 16,
        Sunday = 32
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2024-12-09 02:18:53
var myConst = Expression.Constant(myVal, typeof(Enum));
// here!

exp = Expression.Call(myValTestValidityParam, "HasFlag", null, myConst);

您在找这个吗? HasFlag 想要一个 Enum 作为参数,所以我向下转换了 myVal

var myConst = Expression.Constant(myVal, typeof(Enum));
// here!

exp = Expression.Call(myValTestValidityParam, "HasFlag", null, myConst);

Are you looking for this? HasFlag wants an Enum as the parameter, so I downcasted myVal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文