Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this dynamic parameter is not working? [duplicate]

Tags:

c#

dynamic

Consider this code in a project:

static void Main(string[] args)
{
    DoSomething(new { Name = "Saeed" });
}

public static void DoSomething(dynamic parameters)
{
    Console.WriteLine(parameters.Name);
}

This works like a charm. However, as soon as you separate these two functions into two different projects, the code breaks:

// This code is in a Console Application
static void Main(string[] args)
{
    ExternalClass.DoSomething(new { Name = "Saeed" });
}

// However, this code is in a Class Library; Another project
public class ExternalClass
{

    public static void DoSomething(dynamic parameters)
    {
        Console.WriteLine(parameters.Name);
    }
}

The error I get in the second case is:

object' does not contain a definition for 'Name' (RuntimeBinderException)

Why do I get this error? What's the alternative method? How can I pass a dynamic parameter to a method in another library, and use it there in a simple way?

Note: I'm familiar with ExpandoObject and I don't want to use that.

like image 833
Saeed Neamati Avatar asked Jul 13 '13 11:07

Saeed Neamati


People also ask

What is the dynamic parameter?

Parameter. Dynamic parameters are special types of parameters. Dynamic parameter value is recalculated each time you assess the parameter; i.e., this parameter acts as a function.

How do you create a dynamic parameter in Tableau?

Imitate the Functionality of Dynamic ParametersCreate and format the default dates that will be shown when the workbook is opened. Set up parameters to give the user control over the time period selection. Create date filters to filter the charts to the needed time period. Include these elements on a dashboard.

Can we add all value in parameter to select all the values?

Right click on Region pill, go to create and select Parameter. A parameter configuration pane is created, with the values in Region already populated. All that's needed is to add an “All” value, which can then be dragged to the top of the list. Then click OK.


1 Answers

Your code is using an anonymous type. Originally intended to be useful in a Linq select query expression, added in C# version 3. Unfortunately, the C# compiler generates them with the accessibility modifier for the members in the anonymous type set to internal. Accessibility is enforced by the CLR. Not an issue in Linq since the iterator will be generated in the same assembly as well. Using them with the dynamic keyword was not envisioned, it was added in a later C# version, version 4.

Which means that it will work fine as long as the code is all in the same assembly. However, as soon as you pass the object to code in another assembly, the restrictions enforced by internal kick in and it goes kaboom.

Simply declaring a little public class is the workaround. ExpandoObject works too but adds unnecessary overhead.

like image 185
Hans Passant Avatar answered Oct 03 '22 20:10

Hans Passant