Time Rewizer  v.1.1
Created by: Mark Oostveen
MarkOostveen.TimeRewizer.RewizeUtilityFunctions Class Reference

Utility class for rewinding functions More...

+ Collaboration diagram for MarkOostveen.TimeRewizer.RewizeUtilityFunctions:

Static Public Member Functions

static T CreateInstance< T > ()
 Creates a new instance of the type given More...
 
static T [] CreateInstanceArray< T > (int amount)
 Creates a new instance array of the type given More...
 
static object CreateInstanceByType (Type type, object[] parameters=null)
 Creates an instance by a given type More...
 
static object CreateInstanceArrayByType (Type type, int amount)
 Creates an array of give type More...
 
static T CastTypeExecute< T > (object target)
 Cast object to given generic type More...
 
static object CastType (object target, Type type)
 Cast object to type More...
 

Detailed Description

Utility class for rewinding functions

Member Function Documentation

◆ CastType()

static object MarkOostveen.TimeRewizer.RewizeUtilityFunctions.CastType ( object  target,
Type  type 
)
static

Cast object to type

Parameters
target
type
Returns
162  {
163  MethodInfo method = typeof(RewizeUtilityFunctions).GetMethod("CastTypeExecute");
164 
165  MethodInfo genericMethod = method.MakeGenericMethod(type);
166 
167  return genericMethod.Invoke(null, new[] { target });
168  }

◆ CastTypeExecute< T >()

static T MarkOostveen.TimeRewizer.RewizeUtilityFunctions.CastTypeExecute< T > ( object  target)
static

Cast object to given generic type

Template Parameters
T
Parameters
target
Returns
151  {
152  return (T)target;
153  }

◆ CreateInstance< T >()

static T MarkOostveen.TimeRewizer.RewizeUtilityFunctions.CreateInstance< T > ( )
static

Creates a new instance of the type given

Template Parameters
T
Returns
Type Constraints
T :new() 
25  : new()
26  {
27  return new T();
28  }

◆ CreateInstanceArray< T >()

static T [] MarkOostveen.TimeRewizer.RewizeUtilityFunctions.CreateInstanceArray< T > ( int  amount)
static

Creates a new instance array of the type given

Template Parameters
T
Parameters
amount
Returns
Type Constraints
T :new() 
36  : new()
37  {
38  return new T[amount];
39  }

◆ CreateInstanceArrayByType()

static object MarkOostveen.TimeRewizer.RewizeUtilityFunctions.CreateInstanceArrayByType ( Type  type,
int  amount 
)
static

Creates an array of give type

Parameters
type
amount
Returns
136  {
137  MethodInfo method = typeof(RewizeUtilityFunctions).GetMethod("CreateInstanceArray");
138 
139  MethodInfo genericMethod = method.MakeGenericMethod(type);
140 
141  return genericMethod.Invoke(null, new object[] { amount });
142  }

◆ CreateInstanceByType()

static object MarkOostveen.TimeRewizer.RewizeUtilityFunctions.CreateInstanceByType ( Type  type,
object []  parameters = null 
)
static

Creates an instance by a given type

Parameters
type
parameters
Returns
49  {
50  if (RewizeUtilityFunctions.m_CachedDynamicMethodInfo.ContainsKey(type))
51  {
52  RewizeUtilityFunctions.m_CachedDynamicMethodInfo.TryGetValue(type, out DynamicMethod method);
53  return method?.Invoke(null, parameters);
54  }
55 
56  if (RewizeUtilityFunctions.m_CachedMethodInfo.ContainsKey(type))
57  {
58  RewizeUtilityFunctions.m_CachedMethodInfo.TryGetValue(type, out MethodInfo method);
59  return method?.Invoke(null, parameters);
60  }
61 
62  //check constructor
63  int? amount = parameters?.Length ?? (int?)0;
64  ConstructorInfo ctor = null;
65 
66  ConstructorInfo[] ctors =
67  type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
68  for (int i = 0; i < ctors.Length; i++)
69  {
70  if (ctors[i].GetParameters().Length == amount)
71  {
72  ctor = ctors[i];
73  break;
74  }
75  }
76 
77  if (ctor == null)
78  {
79  MethodInfo method = typeof(RewizeUtilityFunctions).GetMethod("CreateInstance");
80 
81  method = method.MakeGenericMethod(type);
82 
83  RewizeUtilityFunctions.m_CachedMethodInfo.TryAdd(type, method);
84 
85  return method.Invoke(null, null);
86  }
87 
88  return RewizeUtilityFunctions.CilCreateInstanceByType(type, ctor, parameters);
89  }
+ Here is the caller graph for this function: