
- 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 having different datatype with a string value using reflection in C#?
Reflection is when managed code can read its own metadata to find assemblies. Essentially, it allows code to inspect other code within the same system. With reflection in C#, we can dynamically create an instance of a type and bind that type to an existing object. Moreover, we can get the type from an existing object and access its properties. When we use attributes in our code, reflection gives us access as it provides objects of Type that describe modules, assemblies, and types.
Let us say we have a property of type double and in the runtime we actually have the value as string and assign it to the property after changing the type. We can use Convert.ChangeType() - It allows us to use runtime information on any IConvertible type to change representation formats.
Example
using System; using System.Reflection; namespace DemoApplication{ class Program{ static void Main(){ Circle circle = new Circle(); string value = "6.5"; PropertyInfo propertyInfo = circle.GetType().GetProperty("Radius"); propertyInfo.SetValue(circle, Convert.ChangeType(value, propertyInfo.PropertyType), null); var radius = circle.GetType().GetProperty("Radius").GetValue(circle, null); Console.WriteLine($"Radius: {radius}"); Console.ReadLine(); } } class Circle{ public double Radius { get; set; } } }
Output
Radius: 6.5
In the above example we could see that string value "6.5" is converted to actual type double using Convert.ChangeType and assigned to Radius property using reflection in runtime.
- Related Articles
- How to set a property value by reflection in C#?
- How to set NOW() as default value for datetime datatype in MySQL?
- Return a 2-D array with ones on the diagonal and zeros elsewhere but set a different datatype in Numpy
- How to display highest value from a string with numbers set as varchar in MySQL?
- How to retrieve a value with MySQL count() having maximum upvote value?
- How to set a default string value on a Tkinter Spinbox?
- How to fetch a property value dynamically in C#?
- How to create a point chart in R with alternative points having different shape?
- How to set a value in a particular JTable cell with Java?
- Different methods to reverse a string in C/C++
- How to split a string with a string delimiter in C#?
- Return a new array of given shape filled with ones but with different datatype in Numpy
- How to create a barplot with one of the bars having different color in R?
- How to find records with a null value in a set of columns with MySQL
- Sorting binary string having an even decimal value using JavaScript
