0
using System;
public class Program
{
public static void Main()
{
dynamic stud = new Student();
Console.WriteLine(stud.Name);
}
}
public class Student
{
public string Name = "John";
}
Output John
Is there any way to get the same result called the member as a string without modifying the class, something like:
Console.WriteLine(stud["Name"]);
as doubt in itself would be possible, but not at least Name is a Property, can do it by Reflexion, but being a public member who is not Property can not use the method
GetProperty
that would solve this easy, need to go deeper into Reflexion to read the variable ... already as code itself is absolutely meaningless to create adynamic
from a known type (Student), something likevar stud = new Student()
would be best, outside that exposing a public variable is not a good approach, a property would be better there.– Ricardo Pontual
@Ricardopunctual this was just an illustrative example.
– Amadeu Antunes