Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- Class1 cl = new Class1();
- // Get the Type object corresponding to MyClass.
- Type myType = typeof(Class1);
- // Get the PropertyInfo object by passing the property name.
- PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
- // Display the property name.
- myPropInfo.SetValue(cl,10,null);
- Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
- Console.WriteLine("The {0} property exists in MyClass.", cl.MyProperty);
- Console.Read();
- }
- catch (NullReferenceException e)
- {
- Console.WriteLine("The property does not exist in MyClass." + e.Message);
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Class1
- {
- private int myProperty;
- // Declare MyProperty.
- public int MyProperty
- {
- get
- {
- return myProperty;
- }
- set
- {
- myProperty = value;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment