Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass IDictionary<,> as a parameter in attribute, possible?

Tags:

c#

.net

is it possible to pass an IDictionary as an attribute's parameter: like this:

[My(new Dictionary<string,object> { ... }]

or like this:

[My(Data = new Dictionary<string,object> {...} )]

atm I'm thinking to just pass a json string and after transform it into IDictionary<string,object>

like image 321
Omu Avatar asked Mar 29 '11 12:03

Omu


People also ask

How do you pass a dictionary as a parameter in Python?

“ kwargs ” stands for keyword arguments. It is used for passing advanced data objects like dictionaries to a function because in such functions one doesn't have a clue about the number of arguments, hence data passed is be dealt properly by adding “**” to the passing type.

Are dictionaries passed by reference?

In essence, one can say that mutable objects like dictionaries, sets, and lists are passed by reference. Immutable objects like int , str , tuple are passed by value.

Is Python dictionary pass by value or reference?

Python's argument passing model is neither “Pass by Value” nor “Pass by Reference” but it is “Pass by Object Reference”.

How can you pass dictionary to a function?

We can pass a dictionary to a function by passing the name of the dictionary. Let's define a function that accepts a dictionary as a parameter.


2 Answers

No - with attributes you have to pass constant values to them. Perhaps you could have multiple attributes with two properties exposed.

[My(Key = "1234", Value = "1234")]
[My(Key = "4234", Value = "4234")]

When you do your reflection, you could build a dictionary that way.

like image 51
Daniel A. White Avatar answered Oct 03 '22 21:10

Daniel A. White


No, you'll get this error:

Error   20
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
like image 38
Kieren Johnstone Avatar answered Oct 03 '22 23:10

Kieren Johnstone