-6

I have a method that has a dynamic parameter and returns a dynamic result. I would like to be able to pass null, int, string, etc into my method. However I get "NotSupportedException" in all situations.

MyMethod(null); // Causes problems (Should resolve to ref type?)
MyMethod(0); // Causes problems (Should resolve to int type)

public dynamic MyMethod(dynamic b)
{
  if (value != null) {...}// Throws NotSupportedExpception
  if (value != 0) {...} // Throws NotSupportedExpception
}
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
aidesigner
  • 553
  • 2
  • 8
  • 15
  • 5
    Why do you want to use dynamic? – Joe Nov 22 '11 at 22:10
  • 5
    What is "value", and why is "b" not used? This code is very confusing. – Eric Lippert Nov 22 '11 at 22:13
  • 5
    Your method seems to work fine for me (after I renamed `b` to `value`). What is the call stack of the exception? – svick Nov 22 '11 at 22:14
  • 1
    Possible duplicate http://stackoverflow.com/questions/7029699/c-how-to-perform-a-null-check-on-a-dynamic-object – Michał Powaga Nov 22 '11 at 22:18
  • 7
    Svick is right; this code is fine. Post a small *complete* program that *actually demonstrates the problem*. Making it difficult for us to help you diagnose your problem does no one any good. – Eric Lippert Nov 22 '11 at 22:19
  • @EricLippert, can you please regard to this question: http://stackoverflow.com/questions/8234300/linq-to-objects-when-object-is-null-vs-linq-to-sql – gdoron Nov 23 '11 at 07:11
  • The problem is recently in VS2010 I enabled breaking for all exceptions (Under Debug->Exceptions). The exception that is firing is "System.NotSupportedException" under "Common Language Runtime Exceptions". If I continue after this exception or turn it off the code runs correctly. Are not all exceptions bad and should be addressed? Does everyone else turn on exception breaking as I have described? – aidesigner Nov 23 '11 at 19:23
  • why wouldn't you want to use dynamic? – FlavorScape Feb 14 '13 at 00:09
  • As a reference to future fellow googlers - I've encountered a similar problem on Mono v 3.2.0 (not a problem on >=3.4.1). It thrown ArgumentNullException on `dynamic a = 5; if(a==null)...`. – Piotr Zierhoffer May 30 '14 at 10:52

1 Answers1

5

it is just working

    static void Main(string[] args)
    {
        MyMethod(null);
        MyMethod(0);

    }        
    public static dynamic MyMethod(dynamic value)
    {
        if (value != null)
            Console.WriteLine("Value is not null.");
        if (value != 0)
            Console.WriteLine("Value is not 0.");

        return value;
    }

Output

Value is not 0.
Value is not null.
esskar
  • 10,638
  • 3
  • 36
  • 57
  • The problem is recently in VS2010 I enabled breaking for all exceptions (Under Debug->Exceptions). The exception that is firing is "System.NotSupportedException" under "Common Language Runtime Exceptions". If I continue after this exception or turn it off the code runs correctly. Are not all exceptions bad and should be addressed? Does everyone else turn on exception breaking as I have described? – aidesigner Nov 23 '11 at 13:00