Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ‘dynamic’ cannot access properties from anonymous types declared in another assembly

Code below is working well as long as I have class ClassSameAssembly in same assembly as class Program. But when I move class ClassSameAssembly to a separate assembly, a RuntimeBinderException (see below) is thrown. Is it possible to resolve it?

using System;  namespace ConsoleApplication2 {     public static class ClassSameAssembly     {         public static dynamic GetValues()         {             return new             {                 Name = "Michael", Age = 20             };         }     }      internal class Program     {         private static void Main(string[] args)         {             var d = ClassSameAssembly.GetValues();             Console.WriteLine("{0} is {1} years old", d.Name, d.Age);         }     } } 

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Name'

at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at ConsoleApplication2.Program.Main(String[] args) in C:\temp\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 23 
like image 836
mehanik Avatar asked Apr 13 '10 14:04

mehanik


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 C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

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.


1 Answers

I believe the problem is that the anonymous type is generated as internal, so the binder doesn't really "know" about it as such.

Try using ExpandoObject instead:

public static dynamic GetValues() {     dynamic expando = new ExpandoObject();     expando.Name = "Michael";     expando.Age = 20;     return expando; } 

I know that's somewhat ugly, but it's the best I can think of at the moment... I don't think you can even use an object initializer with it, because while it's strongly typed as ExpandoObject the compiler won't know what to do with "Name" and "Age". You may be able to do this:

 dynamic expando = new ExpandoObject()  {      { "Name", "Michael" },      { "Age", 20 }  };  return expando; 

but that's not much better...

You could potentially write an extension method to convert an anonymous type to an expando with the same contents via reflection. Then you could write:

return new { Name = "Michael", Age = 20 }.ToExpando(); 

That's pretty horrible though :(

like image 69
Jon Skeet Avatar answered Oct 13 '22 04:10

Jon Skeet