Before returning the value, GetValue checks to see if the user has access permission. For example, if the field holds a Boolean primitive value, an instance of Object with the appropriate Boolean value is returned. Note that the return type of GetValue is Object. For non-static fields, obj should be an instance of a class that inherits or declares the field. ' Displaying the values of the fields of FieldsClass: using namespace System įieldsClass^ fieldsInst = gcnew FieldsClass Īrray^ fields = fieldsType->GetFields(static_cast(BindingFlags::Public | BindingFlags::Instance)) Ĭonsole::WriteLine("Displaying the values of the fields of :", fieldsType)įor i As Integer = 0 To fields.Length - 1įields(i).Name, fields(i).GetValue(fieldsInst), vbTab) The following example retrieves an array of FieldInfo objects that represents the fields of the FieldsClass type, and then calls the GetValue to display the value of each field for the fieldsInst object. ' The example displays the following output: For Example, you can get the name of the class or you can get details of the private members of the class. With the help of this reflection API, you can inspect classes, constructors, modifiers, fields, methods, and interfaces at runtime. The example displays the following output:įieldInfo fld = typeof(Example).GetField("val") ĭim fld As FieldInfo = GetType(Example).GetField("val") Reflection in Java is to inspect and change the behavior of a program at runtime. For strings, you must first narrow the Object to string (see line 8. using namespace System įieldInfo^ fld = Example::typeid->GetField( "val" ) Ĭonsole::WriteLine(fld->GetValue(nullptr) ) The instanceof operator is used to determine if the array item is an Integer or a String. Note that the value of the obj argument is null. The following example uses the GetValue method to retrieve the value of a static field. The method is neither declared nor inherited by the class of obj.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |