How to set a property value by reflection in C#? - Tutorialspoint
文章推薦指數: 80 %
How to set a property value by reflection in C#? - The System. Reflection namespace contains classes that allow you to obtain information ... TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho HowtosetapropertyvaluebyreflectioninC#? CsharpServerSideProgrammingProgramming TheSystem.Reflectionnamespacecontainsclassesthatallowyoutoobtaininformationabouttheapplicationandtodynamicallyaddtypes,values,andobjectstotheapplication.Reflectionobjectsareusedforobtainingtypeinformationatruntime.TheclassesthatgiveaccesstothemetadataofarunningprogramareintheSystem.Reflectionnamespace.Reflectionallowsviewattributeinformationatruntime.Reflectionallowsexaminingvarioustypesinanassemblyandinstantiatethesetypes.Reflectionallowslatebindingtomethodsandproperties.Reflectionallowscreatingnewtypesatruntimeandthenperformssometasksusingthosetypes.ExampleGetProperty(String)Searchesforthepublicpropertywiththespecifiedname.GetType(String,Boolean)GetstheTypeobjectwiththespecifiednameintheassemblyinstanceandoptionallythrowsanexceptionifthetypeisnotfound.SetValue(Object,Object)Setsthepropertyvalueofaspecifiedobject.classProgram{ staticvoidMain(string[]args){ Useruser=newUser(); Typetype=user.GetType(); PropertyInfoprop=type.GetProperty("Name"); prop.SetValue(user,"Bangalore",null); System.Console.WriteLine(user.Name); Console.ReadLine(); } } classUser{ publicintId{get;set;} publicstringName{get;set;} }OutputBangalore NizamuddinSiddiqui Publishedon05-Nov-202013:53:15 RelatedQuestions&AnswersHowtosetapropertyhavingdifferentdatatypewithastringvalueusingreflectioninC#? ReflectioninC# HowtofetchapropertyvaluedynamicallyinC#? MirrorReflectioninC++ LineReflectioninC++ HowtodeleteanelementfromtheSetbypassingitsvalueinC++ HowtouseReflectioninGolang? SetacertainvaluefirstwithMySQLORDERBY? HowtodisplaymethodsandpropertiesusingreflectioninC#? HowtosetselecteditemofSpinnerbyvalueinsteadofbypositiononAndroid? HowtoaddareflectioneffecttoatextnodeinJavaFX? HowtopreventReflectiontobreakaSingletonClassPattern? HowdoyougiveaC#Auto-Propertyadefaultvalue? HowtosortaPythondictionarybyvalue? HowtoinstantiateastaticinnerclasswithreflectioninJava? PreviousPage PrintPage NextPage Advertisements Print AddNotes Bookmarkthispage ReportError Suggestions Save Close Dashboard Logout
延伸文章資訊
- 1[C#] 利用反射動態設定物件屬性Property SetValue in Reflection
- 2PropertyInfo.SetValue 方法(System.Reflection) | Microsoft Docs
StaticProperty); // Get a type object that represents the Example type. Type examType = typeof(Ex...
- 3C# Properties (Get and Set) - W3Schools
The set method assigns a value to the name variable. The value keyword represents the value we as...
- 4Set Default Value to Property in C# - TutorialsTeacher
Set Default Value to Property in C# · Default Value of Auto-Implemented Property · Using Property...
- 5使用屬性- C# 程式設計手冊 - Microsoft Docs
class Person { private string _name; // the name field public string Name // the Name property { ...