How to declare a class dynamically? WITH#

is it possible to declare a class dynamically? is it possible to create a shared list with an anonymous class in c #? any code snippets will help. thank

+3
source share
3 answers

Microsoft made C # dynamic in version 4.0. You can use the new keyword "dynamic". The following link contains some good examples of how to use the new dynamic type.

http://msdn.microsoft.com/en-us/library/dd264741.aspx

+1
source

A class declaration dynamically requires CodeDom .

is it possible to create a shared list with an anonymous class in c #?

, , , . , :

var range = Enumerable.Range(0, 100);

var genericList = range.Select(value => new { Value = value }).ToList();

genericList List<T>, .

+5

As mentioned in the SLaks comments, this is possible. But this is not trivial. I'm not sure what you are trying to do, but you can easily add anonymous types to the general list of objects.

List<object> list = new List<object>();
for(int i = 0; i < 10; i++){
   list.Add(new { SomeProperty = i, OtherProperty = "foobar" });
}
+5
source

Source: https://habr.com/ru/post/1772689/


All Articles