Empeld
Empeld plugin documentation.
pluginbase.Helpers.Extensions.ReflectionExtensions Class Reference

Classes

class  UnifiedVariable
 

Public Member Functions

delegate void UnifiedSetter (object obj, object val)
 
delegate object UnifiedGetter (object obj)
 

Static Public Member Functions

static UnifiedVariable GetUnifiedVariable (this PropertyInfo realProp)
 Turns a property into a unified variable More...
 
static UnifiedVariable GetUnifiedVariable (this FieldInfo realField)
 Turns a field into a unified variable More...
 
static IEnumerable< UnifiedVariableGetMemberVars (this Type type, BindingFlags binding, Predicate< MemberInfo > predicate)
 Gets the members vars (Fields and Properties) of a type More...
 
static UnifiedVariable GetMemberVar (this Type type, BindingFlags flags, string name)
 Get a property (takes precedence) or a field. Returns unified variable. if not found More...
 
static UnifiedVariable GetMemberVar (this Type type, string name)
 Get a property (takes precedence) or a field with default binding flags. Returns null if not found More...
 
static IEnumerable< UnifiedVariableGetAllMemberVars (this Type type, Predicate< MemberInfo > predicate)
 Gets all member vars, including privates, of each class More...
 
static IEnumerable< Type > GetImplementingClasses (this Type type)
 Gets all classes and superclasses, starting from the top-level class and going to base class More...
 
static IEnumerable< MethodInfo > GetAllMethods (this Type type, Predicate< MethodInfo > predicate)
 Finds all methods in a type, included private ones, that satisfy the predicate More...
 
static IEnumerable< MethodInfo > GetAllMethods (this Type type)
 Finds all methods in a type, included private ones, that satisfy the predicate More...
 

Public Attributes

const BindingFlags DEFAULT_BINDING_FLAGS = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
 

Member Function Documentation

◆ GetAllMemberVars()

static IEnumerable<UnifiedVariable> pluginbase.Helpers.Extensions.ReflectionExtensions.GetAllMemberVars ( this Type  type,
Predicate< MemberInfo >  predicate 
)
static

Gets all member vars, including privates, of each class

Returns
The all member variables.
Parameters
typeType.
predicatePredicate.
144  {
145  //To get all member types, we need to dive into each implementation of the type
146  var vars = new HashSet<UnifiedVariable>();
147 
148  foreach(var subtype in GetImplementingClasses(type))
149  {
150  foreach(var member in GetMemberVars(subtype, DEFAULT_BINDING_FLAGS, predicate))
151  {
152  if (vars.Add(member))
153  yield return member;
154  }
155  }
156  }
static IEnumerable< Type > GetImplementingClasses(this Type type)
Gets all classes and superclasses, starting from the top-level class and going to base class ...
Definition: ReflectionExtensions.cs:163
const BindingFlags DEFAULT_BINDING_FLAGS
Definition: ReflectionExtensions.cs:10
static IEnumerable< UnifiedVariable > GetMemberVars(this Type type, BindingFlags binding, Predicate< MemberInfo > predicate)
Gets the members vars (Fields and Properties) of a type
Definition: ReflectionExtensions.cs:89

◆ GetAllMethods() [1/2]

static IEnumerable<MethodInfo> pluginbase.Helpers.Extensions.ReflectionExtensions.GetAllMethods ( this Type  type,
Predicate< MethodInfo >  predicate 
)
static

Finds all methods in a type, included private ones, that satisfy the predicate

Returns
The all methods.
Parameters
typeType.
predicatePredicate.
180  {
181  foreach(var cls in GetImplementingClasses(type))
182  {
183  foreach(var method in cls.GetMethods(DEFAULT_BINDING_FLAGS))
184  {
185  if (method.DeclaringType == cls && predicate(method))
186  {
187  yield return method;
188  }
189  }
190  }
191  }
static IEnumerable< Type > GetImplementingClasses(this Type type)
Gets all classes and superclasses, starting from the top-level class and going to base class ...
Definition: ReflectionExtensions.cs:163
const BindingFlags DEFAULT_BINDING_FLAGS
Definition: ReflectionExtensions.cs:10

◆ GetAllMethods() [2/2]

static IEnumerable<MethodInfo> pluginbase.Helpers.Extensions.ReflectionExtensions.GetAllMethods ( this Type  type)
static

Finds all methods in a type, included private ones, that satisfy the predicate

Returns
The all methods.
Parameters
typeType.
199  {
200  return GetAllMethods(type, x => true);
201  }
static IEnumerable< MethodInfo > GetAllMethods(this Type type, Predicate< MethodInfo > predicate)
Finds all methods in a type, included private ones, that satisfy the predicate
Definition: ReflectionExtensions.cs:179

◆ GetImplementingClasses()

static IEnumerable<Type> pluginbase.Helpers.Extensions.ReflectionExtensions.GetImplementingClasses ( this Type  type)
static

Gets all classes and superclasses, starting from the top-level class and going to base class

Returns
The implementing classes.
Parameters
typeType.
164  {
165  Type curr = type;
166  do
167  {
168  yield return curr;
169  curr = curr.BaseType;
170  } while(curr != null);
171  }

◆ GetMemberVar() [1/2]

static UnifiedVariable pluginbase.Helpers.Extensions.ReflectionExtensions.GetMemberVar ( this Type  type,
BindingFlags  flags,
string  name 
)
static

Get a property (takes precedence) or a field. Returns unified variable. if not found

Returns
The member variable.
Parameters
typeType.
flagsFlags.
nameName.
112  {
113  var prop = type.GetProperty(name, flags);
114  if (prop != null)
115  {
116  return prop.GetUnifiedVariable();
117  }
118  var field = type.GetField(name, flags);
119  if (field != null)
120  {
121  return field.GetUnifiedVariable();
122  }
123  return null;
124  }

◆ GetMemberVar() [2/2]

static UnifiedVariable pluginbase.Helpers.Extensions.ReflectionExtensions.GetMemberVar ( this Type  type,
string  name 
)
static

Get a property (takes precedence) or a field with default binding flags. Returns null if not found

Returns
The member variable.
Parameters
typeType.
nameName.
133  {
134  return GetMemberVar(type, DEFAULT_BINDING_FLAGS, name);
135  }
const BindingFlags DEFAULT_BINDING_FLAGS
Definition: ReflectionExtensions.cs:10
static UnifiedVariable GetMemberVar(this Type type, BindingFlags flags, string name)
Get a property (takes precedence) or a field. Returns unified variable. if not found ...
Definition: ReflectionExtensions.cs:111

◆ GetMemberVars()

static IEnumerable<UnifiedVariable> pluginbase.Helpers.Extensions.ReflectionExtensions.GetMemberVars ( this Type  type,
BindingFlags  binding,
Predicate< MemberInfo >  predicate 
)
static

Gets the members vars (Fields and Properties) of a type

Returns
The member variables.
Parameters
typeType.
bindingBinding.
predicatePredicate.
90  {
91  foreach(var prop in type.GetProperties(binding).Where(x => predicate(x)))
92  {
93  var realProp = prop.DeclaringType.GetProperty(prop.Name, binding);
94  yield return realProp.GetUnifiedVariable();
95  }
96 
97  foreach(var field in type.GetFields(binding).Where(x => predicate(x)))
98  {
99  var realField = field.DeclaringType.GetField(field.Name, binding);
100  yield return realField.GetUnifiedVariable();
101  }
102  }

◆ GetUnifiedVariable() [1/2]

static UnifiedVariable pluginbase.Helpers.Extensions.ReflectionExtensions.GetUnifiedVariable ( this PropertyInfo  realProp)
static

Turns a property into a unified variable

Returns
The unified variable.
Parameters
realPropReal property.
48  {
49  var getMethod = realProp.GetGetMethod(true);
50 
51  return new UnifiedVariable {
52  Type = realProp.PropertyType,
53  Member = realProp,
54  Setter = realProp.GetSetMethod(true) != null ? (o, v) => realProp.SetValue(o, v, null) : (UnifiedSetter)null,
55  Getter = (o) => realProp.GetValue(o, null),
56  IsPrivate = getMethod.IsPrivate,
57  IsProtected = getMethod.IsFamily,
58  IsReadonly = false,
59  IsField = false
60  };
61  }
delegate void UnifiedSetter(object obj, object val)

◆ GetUnifiedVariable() [2/2]

static UnifiedVariable pluginbase.Helpers.Extensions.ReflectionExtensions.GetUnifiedVariable ( this FieldInfo  realField)
static

Turns a field into a unified variable

Returns
The unified variable.
Parameters
realFieldReal field.
69  {
70  return new UnifiedVariable {
71  Type = realField.FieldType,
72  Member = realField,
73  Setter = realField.SetValue,
74  Getter = realField.GetValue,
75  IsPrivate = realField.IsPrivate,
76  IsReadonly = realField.IsInitOnly,
77  IsProtected = realField.IsFamily,
78  IsField = true,
79  };
80  }

◆ UnifiedGetter()

delegate object pluginbase.Helpers.Extensions.ReflectionExtensions.UnifiedGetter ( object  obj)

◆ UnifiedSetter()

delegate void pluginbase.Helpers.Extensions.ReflectionExtensions.UnifiedSetter ( object  obj,
object  val 
)

Member Data Documentation

◆ DEFAULT_BINDING_FLAGS

const BindingFlags pluginbase.Helpers.Extensions.ReflectionExtensions.DEFAULT_BINDING_FLAGS = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance

The documentation for this class was generated from the following file: