How can I Get the property by name from type and Set Propert value using reflection in C#

linos

I'm learning about C# reflection and ran into some road blocks along the way. I have an example problem where I'm trying to perform the following:

  1. Create new instance of Student - That I understand Student sT = new Student()
  2. Get instance type - That I understand: var getType = sT.GetType();
  3. Get property FullName by name from type - How can I achieve this??
  4. Set property value to "Some Name" using reflection. How can I achieve this??
using System;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        //1. Create new instance of Stuedent

        Student sT = new Student();

        //2. Get instance type

        var getType = sT.GetType();

        var myStringProperties1 = getType.GetProperty("FullName",
                typeof(string));

        //3. Get property FullName by name from type  ????

        //4.  Set property value to "Some Name" using reflection        

    }
}

public class Student
{
    public string FullName { get; set; }

    public int Class { get; set; }

    public DateTime DateOfBirth { get; set; }

    public string GetCharacteristics()
    {
        return "";
    }
}

Thanks in advance.

lsedlacek

You're almost there; now just call SetValue on the property info:

//1. Create new instance of Student
Student student = new Student();

//2. Get instance type
var getType = student.GetType();

//3. Get property FullName by name from type
var fullNameProperty = getType.GetProperty("FullName",
        typeof(string));

//4.  Set property value to "Some Name" using reflection
fullNameProperty.SetValue(student, "Some Name");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get a property name and its value using Swift 2.0, and reflection?

From Dev

How to get value of property from object of class at runtime using reflection

From Dev

How to use reflection to get/set the value of a property?

From Dev

How to get Fully Qualified Name of a Type using Reflection in C#

From Dev

How to set the value of a property to null using reflection

From Java

Get property value from string using reflection

From Dev

How can I get the Object from a Field/Class using reflection?

From Dev

Can I set an Objective-C property of Int enum type, from Swift using an Int

From Dev

Get own property name from parent class using reflection

From Dev

How can I check if a property has been set using Swift reflection?

From Dev

How to get the property name set to false value

From Dev

How can I get a protected method via reflection matching name and parameters type?

From Dev

Property reflection - How to get value?

From Dev

How can I use reflection to set this object's property?

From Dev

How can I find the type of a property dynamically in swift (Reflection/Mirror)?

From Dev

How can I find the type of a property dynamically in swift (Reflection/Mirror)?

From Dev

How to get type of property using System.Reflection.Metadata?

From Dev

How can I get class information by property w/ reflection C#

From Java

How can I hint the C# 8.0 nullable reference system that a property is initalized using reflection

From Dev

Using Reflection.Emit to set a property value

From Dev

How can I get the name of a Kotlin property?

From Dev

How can I get a value from a JSON with a partially field name

From Dev

How can I get every value name from a Model List

From Dev

How to get method name from inside that method without using reflection in C#

From Dev

how can I get/set a value from interface of a map?

From Dev

how to get property Value by knowing its name from a string having type hierarichy

From Dev

How to get the properties from a dynamic (Anonymous Type) object using reflection?

From Dev

How to get the properties from a dynamic (Anonymous Type) object using reflection?

From Dev

How to get list type elements using reflection in c#

Related Related

  1. 1

    How to get a property name and its value using Swift 2.0, and reflection?

  2. 2

    How to get value of property from object of class at runtime using reflection

  3. 3

    How to use reflection to get/set the value of a property?

  4. 4

    How to get Fully Qualified Name of a Type using Reflection in C#

  5. 5

    How to set the value of a property to null using reflection

  6. 6

    Get property value from string using reflection

  7. 7

    How can I get the Object from a Field/Class using reflection?

  8. 8

    Can I set an Objective-C property of Int enum type, from Swift using an Int

  9. 9

    Get own property name from parent class using reflection

  10. 10

    How can I check if a property has been set using Swift reflection?

  11. 11

    How to get the property name set to false value

  12. 12

    How can I get a protected method via reflection matching name and parameters type?

  13. 13

    Property reflection - How to get value?

  14. 14

    How can I use reflection to set this object's property?

  15. 15

    How can I find the type of a property dynamically in swift (Reflection/Mirror)?

  16. 16

    How can I find the type of a property dynamically in swift (Reflection/Mirror)?

  17. 17

    How to get type of property using System.Reflection.Metadata?

  18. 18

    How can I get class information by property w/ reflection C#

  19. 19

    How can I hint the C# 8.0 nullable reference system that a property is initalized using reflection

  20. 20

    Using Reflection.Emit to set a property value

  21. 21

    How can I get the name of a Kotlin property?

  22. 22

    How can I get a value from a JSON with a partially field name

  23. 23

    How can I get every value name from a Model List

  24. 24

    How to get method name from inside that method without using reflection in C#

  25. 25

    how can I get/set a value from interface of a map?

  26. 26

    how to get property Value by knowing its name from a string having type hierarichy

  27. 27

    How to get the properties from a dynamic (Anonymous Type) object using reflection?

  28. 28

    How to get the properties from a dynamic (Anonymous Type) object using reflection?

  29. 29

    How to get list type elements using reflection in c#

HotTag

Archive