Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, how to access curly brace constructor using reflection?

In C# we can now construct new objects using the curly brace constructor, i.e.

class Person {
   readonly string FirstName {get; set;}
   readonly string LastName {get; set;}
}

new Person { FirstName = "Bob", LastName = "smith" }

I need to construct this object using reflection, but if these member variables are marked readonly, I can only set them in the constructor, and there is only the curly-brace constructor available. Is there some way I can access the curly-brace-style constructor using reflection? Thanks.

like image 725
nganju Avatar asked Feb 28 '11 19:02

nganju


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.


2 Answers

First, properties cannot be marked read-only with the readonly keyword. They can only have non-public setter methods*).

Second, there's no such thing as a “curly brace constructor”. It's just a syntactic sugar (a shortcut) for a set of statements like this:

Person p = new Person();  // standard parameterless constructor is called
p.FirstName = "Bob";
p.LastName = "Smith";

Note that you could also use a constructor with parameters:

new Person(1, 2, 3) { FirstName = "Bob", LastName = "Smith" }

gets translated as:

Person p = new Person(1, 2, 3);
p.FirstName = "Bob";
p.LastName = "Smith";

Regarding reflection: To construct a new instance and initialize it in the same way as in the new Person { FirstName = "Bob", LastName = "Smith" } example you have to:

  1. retrieve the constructor (see Type.GetConstructor) and call it or use Activator.CreateInstance to instantiate the type,
  2. retrieve the FirstName property (see Type.GetProperty),
  3. set it (see PropertyInfo.SetValue),
  4. repeat (2) and (3) for LastName.

*) Update: C# 9 will (as of May 2020) introduce init-only properties, which will be the equivalent of readonly fields, allowing property assignment during object initialization only (i.e. in the constructor or in an object initializer).

like image 94
Ondrej Tucny Avatar answered Oct 11 '22 06:10

Ondrej Tucny


This has no relation to constructor. This is simply short-handed syntax to initialize normal properties.

new Person { FirstName = "Bob", LastName = "smith" };

is exactly same as

var person = new Person();
person.FirstName = "Bob";
person.LastName = "smith";

And as people said. Your example wont compile. Because its not constructor.

like image 27
Euphoric Avatar answered Oct 11 '22 07:10

Euphoric