Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. Type t = Type.GetType("WebCore.Models.Page");
  2. object page = new t();
  3.  
  4. public T GetInstance<T>(string type)
  5. {
  6. return (T)Activator.CreateInstance(Type.GetType(type));
  7. }
  8.  
  9. T CreateType<T>() where T : new()
  10. {
  11. return new T();
  12. }
  13.  
  14. using System;
  15. using System.Runtime.Remoting;
  16.  
  17. class Program
  18. {
  19. static void Main()
  20. {
  21. ObjectHandle o = Activator.CreateInstance("mscorlib.dll", "System.Int32");
  22.  
  23. Int32 i = (Int32)o.Unwrap();
  24. }
  25. }
  26.  
  27. public static T GetInstance<T>(params object[] args)
  28. {
  29. return (T)Activator.CreateInstance(typeof(T), args);
  30. }
  31.  
  32. public class Counter<T>
  33. {
  34. public T Value { get; set; }
  35. }
  36.  
  37. string typeName = typeof(Counter<>).AssemblyQualifiedName;
  38. Type t = Type.GetType(typeName);
  39.  
  40. Counter<int> counter =
  41. (Counter<int>)Activator.CreateInstance(
  42. t.MakeGenericType(typeof(int)));
  43.  
  44. counter.Value++;
  45. Console.WriteLine(counter.Value);
  46.  
  47. public static T Clone<T>(T original)
  48. {
  49. T newObject = (T)Activator.CreateInstance(original.GetType());
  50.  
  51. foreach (var prop in original.GetType().GetProperties())
  52. {
  53. prop.SetValue(newObject, prop.GetValue(original));
  54. }
  55.  
  56. return newObject;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement