
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to set a property value by reflection in C#?
The System. Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.
Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System. Reflection namespace.
Reflection allows view attribute information at runtime.
Reflection allows examining various types in an assembly and instantiate these types.
Reflection allows late binding to methods and properties.
Reflection allows creating new types at runtime and then performs some tasks using those types.
Example
GetProperty(String)
Searches for the public property with the specified name.
GetType(String, Boolean)
Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.
SetValue(Object, Object)
Sets the property value of a specified object.
class Program{ static void Main(string[] args){ User user = new User(); Type type = user.GetType(); PropertyInfo prop = type.GetProperty("Name"); prop.SetValue(user, "Bangalore", null); System.Console.WriteLine(user.Name); Console.ReadLine(); } } class User{ public int Id { get; set; } public string Name { get; set; } }
Output
Bangalore
- Related Articles
- How to set a property having different datatype with a string value using reflection in C#?
- How to fetch a property value dynamically in C#?
- How to set a value to a file input in HTML?
- How to set result of a java expression in a property in JSP?
- How to set selected item of Spinner by value instead of by position on Android?
- How to set Octet value in JavaTuples
- How to delete an element from the Set by passing its value in C++
- Set a certain value first with MySQL ORDER BY?
- How to use Reflection in Golang?
- How to Get Current Value of a CSS Property in JavaScript?
- How to set default Field Value in MySQL?
- How to set a value in a particular JTable cell with Java?
- How to set default value to NULL in MySQL?
- How to set the sticky button property properly?
- How to set dynamic property keys to an object in JavaScript?
