0

I have been following this question on how to use anonymous for IDictionary since it provides a cleaner and compact code solution.

Difference between anonymous class and IDictionary<string,object> for htmlAttributes in ASP.NET MVC?

When i was trying to test how it works, i get the following errors from my compilter:

(23:13) The best overloaded method match for 'Rextester.Program.test(System.Collections.Generic.IDictionary<string,string>)' has some invalid arguments
(23:18) Argument 1: cannot convert from 'AnonymousType#1' to 'System.Collections.Generic.IDictionary<object,string>'

Here is my code:

namespace Rextester
{
  public class Program
  {        
     public static void test(IDictionary<object,string> carl){}       

     public static void Main(string[] args)
     {
        test(new{carl="Hello", carl2="World"});
     }
   }
 }

Can someone tell me what am i doing wrong? I even copy pasted the code from the other SO but i still keep on getting the same error.

Compiler: I am at school so I am just using an online compiler.

Community
  • 1
  • 1
Carlos Miguel Colanta
  • 2,685
  • 3
  • 31
  • 49

3 Answers3

2

MVC does not convert an anonymous object to an IDictionary. Notice that there are two methods in the linked question:

public static MvcHtmlString TextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    IDictionary<string, object> htmlAttributes);

public static MvcHtmlString TextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    object htmlAttributes);

The last parameters to the first method is an IDictionary<string, object> while the last parameter to the second method is object. When you pass an anonymous object to TextBoxFor it is the second method that is called since an anonymous object can be converted to object, but not IDictionary<string, object>. It would then use reflection to get the properties and values defined on the anonymous type.

See In c# convert anonymous type into key/value array? for how to convert an anonymous type to and IDictionary<string, object>.

Community
  • 1
  • 1
shf301
  • 31,086
  • 2
  • 52
  • 86
  • So if i have something like `public static string Label(this HtmlHelper htmlHelper, string forName, string labelText, IDictionary htmlAttributes)` then i must pass it as strictly `Dictionary` only ? – Carlos Miguel Colanta Sep 08 '15 at 21:26
  • Yes you can only pass a `Dictionary` for `htmlAttributes` to that method. But you can have a separate method `public static string Label(this HtmlHelper htmlHelper, string forName, string labelText, object htmlAttributes)` that you could pass an anonymous type to. – shf301 Sep 08 '15 at 21:33
  • Argh im so slow, totally forgot about reflection. I can just convert the anonymous and redirect it to the one with IDictionary. Did i get the idea correct? – Carlos Miguel Colanta Sep 08 '15 at 21:35
1

According to https://msdn.microsoft.com/en-us/library/bb397696.aspx: "Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object."

So I guess the compiler cannot cast it to IDictionary<> for the method call, which is what the compiler tells you.

azt
  • 2,100
  • 16
  • 25
1

Try this

public static void test(IDictionary<object, string> carl)
        {


        }


        public static void Main(string[] args)
        {
            test( new Dictionary<object,string>{{"Hello", "World"}});
        }​
jdweng
  • 33,250
  • 2
  • 15
  • 20