Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compilation error with LINQ and dynamic inheritance

Consider the following code (for the sake of this test, it doesn't do anything of particular use - it's just to demonstrate the error that occurs)

Dictionary<string, dynamic> d = new Dictionary<string, dynamic>()
{
    { "a", 123 },
    { "b", Guid.NewGuid() },
    { "c", "Hello World" }
};
d.Where(o => o.Key.Contains("b")).ForEach(i => Console.WriteLine(i.Value));
//retuns the Guid value, as expected.

I want to wrap Dictionary<string, dynamic> using inheritance:

public class CustomDictionary : Dictionary<string, dynamic>
{
}

Here is the examle above using this derived class:

CustomDictionary d = new CustomDictionary()
{
    { "a", 123 },
    { "b", Guid.NewGuid() },
    { "c", "Hello World" }
};
d.Where(o => o.Key.Contains("b")).ForEach(i => Console.WriteLine(i.Value));

This happens...

enter image description here

enter image description here

Any ideas on what is causing the issue, or how to solve it?

like image 590
Matthew Layton Avatar asked Dec 16 '14 22:12

Matthew Layton


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

I believe I have narrowed this down the binding to the Linq Where extension method.

This works:

   d.AsEnumerable()
    .Where(o => o.Key.Contains("b"))
    .ToList()
    .ForEach(i => Console.WriteLine(i.Value));

And this works (calling the extension method statically):

Enumerable.Where(d.AsEnumerable(),o => o.Key.Contains("b"))
          .ToList()
          .ForEach(i => Console.WriteLine(i.Value));

but this does not:

   d.Where(o => o.Key.Contains("b"))
    .ToList()
    .ForEach(i => Console.WriteLine(i.Value));

If I call the static extension method without AsEnumerable():

Enumerable.Where(d,o => o.Key.Contains("b"))
          .ToList()
          .ForEach(i => Console.WriteLine(i.Value));

I get the better compiler error:

Argument 1: cannot convert from 'UserQuery.CustomDictionary' to 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,dynamic>>'

So for some reason, the compiler is not able to bind the inherited class to the extension method.

The following methods also work:

  • explicitly casting d to IEnumerable<System.Collections.Generic.KeyValuePair<string,dynamic>>
  • using object instead of dynamic
like image 151
D Stanley Avatar answered Oct 06 '22 00:10

D Stanley