Guest User

Untitled

a guest
Jul 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. namespace ConsoleApplication1
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. try
  8. {
  9. Class1 cl = new Class1();
  10. // Get the Type object corresponding to MyClass.
  11. Type myType = typeof(Class1);
  12. // Get the PropertyInfo object by passing the property name.
  13. PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
  14. // Display the property name.
  15. myPropInfo.SetValue(cl,10,null);
  16. Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
  17. Console.WriteLine("The {0} property exists in MyClass.", cl.MyProperty);
  18. Console.Read();
  19. }
  20. catch (NullReferenceException e)
  21. {
  22. Console.WriteLine("The property does not exist in MyClass." + e.Message);
  23. }
  24. }
  25. }
  26. }
  27.  
  28.  
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Text;
  34.  
  35. namespace ConsoleApplication1
  36. {
  37. class Class1
  38. {
  39. private int myProperty;
  40. // Declare MyProperty.
  41. public int MyProperty
  42. {
  43. get
  44. {
  45. return myProperty;
  46. }
  47. set
  48. {
  49. myProperty = value;
  50. }
  51. }
  52. }
  53. }
Add Comment
Please, Sign In to add comment