Skip to content

Day 04 Reflection

Kobi Hari edited this page Oct 22, 2020 · 1 revision

Day 04 - Reflection

Projects:

Fun With Reflection Deep Dive into the Reflection
Fun With Expressions Intro to LINQ Expressions

Reflection Basics

  • We have seen how to get the type object from an instance using GetType() or from type using typeof
  • We have seen how to query the members of the type: Properties, Methods, Fields, and Events
  • We used the Is**** properties to query specific aspects of a member such as access level, static or not, and so on.
  • We have seen how to invoke a method using reflection MethodInfo
  • We have seen how to set the value of a property using PropertyInfo
  • We have seen how to instantiate an object using the Activcator

Reflection Generics

  • We have defined the difference between an Open Type (A.K.A Generic Type Definition) and a Closed Type
  • We saw how to extract the Type object for open and closed types
  • We have seen how to get the open type from a closed type using Type.GetGenericTypeDefinition()
  • We have seen how to generate a closed type from an open type using Type.MakeGenericType and providing the type arguments

Attributes

  • We created a new custom attribute by inheriting a class from the Attribute base class
  • We used the AttributeUsage attribute to define where our attribute may be used
  • We defined alternative constructors for our custom attribute to accept additional data into the attribute instance
  • We used the Type.GetCustomAttributes<T> method to query the attributes that were placed on a method and check if a method is decorated with an attribute

Introduction to Expressions

  • We explained that method invokation through reflection is expensive
  • We saw how we can get the compiler to build an expression tree out of a lambda expression literal

T.B.C