InitObejct.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace SAGA.DotNetUtils.Data
  6. {
  7. public class InitObejct
  8. {
  9. public InitObejct()
  10. {
  11. this.Init();
  12. }
  13. private void Init()
  14. {
  15. foreach (PropertyInfo info in base.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
  16. {
  17. try
  18. {
  19. Type type = info.PropertyType;
  20. if (type.IsClass && !type.IsAbstract)
  21. {
  22. InitAttribute[] customAttributes = info.GetCustomAttributes(typeof(InitAttribute), true) as InitAttribute[];
  23. if (customAttributes.Any<InitAttribute>())
  24. {
  25. if (type == typeof(string))
  26. {
  27. info.SetValue(this, string.Empty, null);
  28. }
  29. else
  30. {
  31. ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
  32. if (constructor != null)
  33. {
  34. info.SetValue(this, constructor.Invoke(null), null);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. catch (Exception)
  41. {
  42. }
  43. }
  44. }
  45. }
  46. }