2

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
    }
}
serhio
  • 28,010
  • 62
  • 221
  • 374
  • What should the expression do? – xanatos Aug 29 '11 at 14:33
  • @Steven: Please read till the end... to-xanatos: The expression evaluates if a enum HasFlag, as you can see from code. – serhio Aug 29 '11 at 14:34
  • I don't see a question either. – BoltClock Aug 29 '11 at 14:37
  • I can't see where there is an instance of `TestHehe` here? – Tim Lloyd Aug 29 '11 at 14:39
  • @BoltClock: if my last post phrase is not good enough, read it as "What is wrong with this code?" – serhio Aug 29 '11 at 14:39
  • 1
    If you want anyone to help you with *your* problem, please respect them by coming up with a clear description of your problem. Tell us what the problem is, what you tried so far. "What's wrong with this code" will never cut it. Why doesn't it work for you? Do you get an compiler error (if so, what error), or do you get a runtime exception (it so, please post the stack trace) or doesn't it behave the way you expect it to? – Steven Aug 29 '11 at 14:41
  • @chibacity: perhaps you can't see it. because there is no instance of TestHehe. No need. I just write an expression that will test objects of type TestHehe. – serhio Aug 29 '11 at 14:41
  • 1
    @Steven The error message is in the source code example... – Tim Lloyd Aug 29 '11 at 14:44
  • @Steven I updated the post. But as for me, if you read the code, it is self explanatory... Surely, for people that understands it... – serhio Aug 29 '11 at 14:48
  • @serhio: Please read [this](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) before posting another question here. – Steven Aug 29 '11 at 17:23
  • @Steven: Some people understood easier, others does not so. Not worth to comment not knowing the subject... – serhio Aug 31 '11 at 10:28

1 Answers1

3
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.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • so, what do you want do say... Validity is an Enum too! why it does not find an inherited type parameter?! – serhio Aug 29 '11 at 14:50
  • @serhio the signature of HasFlag is `bool HasFlag(Enum)`. Perhaps `Expression.Call` doesn't try to check for the base types of parameters, or perhaps it's because `enums` are strange beasts in .NET . I don't know. – xanatos Aug 29 '11 at 14:53