You can also look for a code for this article (and previous one) on github for more information.Īll variables with names starting with 't' will have value of newly created instance. Both are explained in the previous article in series. If you are wondering what is Type variable above - it is instance of TestClass, which is class used for testing all of delegates. Var c8 = typeof(TestStruct).Contructor( typeof( int)) Var c7 = typeof(TestStruct).Contructor>() Var cd = DelegateFactory.DefaultContructor() This will look much clearer as a lambda for constructor with single parameter. Created instance is then converted to object if is of value type. Collections of processed parameters is then used as a collection of arguments for constructor. Objects are converted to correct type of parameter at the same index. It returns delegate that takes array of objects and returns object. Var paramsExpression = new Expression įor ( var i = 0 i )Expression.Lambda(returnExpression, argsArray).Compile() Var argsArray = Expression.Parameter( typeof(object)) Var constructorInfo = GetConstructorInfo(source, ctrArgs) Public static Func Contructor( this Type source, params Type ctrArgs) So instead we should write more general method that takes array of types for constructor, exactly how this was done with indexers in the previous article. But it stills limits us to number of parameters allowed in Func class (however if code have methods with that many parameters there are probably more important problems than this ) and it could return constructor different than expected (there can be more than one with given number of parameters). Of course we could create method that returns delegate with correct number of parameters as in constructor, except with not known types changed to object. This is similar problem to the one, that forced us to create Constructor overload - we cannot really know what type of delegate should be constructed if we do not know types of arguments. Next problem arise when we do not have access to all types of parameters - not known type methods can have parameters that are also unknown or unavailable. Var c = typeof(TestStruct).Contructor>() įunc d = i => ( object) new TestStruct(i) But, as before with other members, we have to add separate method to obtain constructors by reflection. What we need to start? Since every type have at least one constructor, even if it is not defined explicitly, we do not need to add any code to our TestClass (from previous article). Since this case is less complex we will discuss this first. For this cases we will look for default constructor, without parameters. Of course very often types do not have any special constructor (declared by user). Since constructors can differ by parameters types, we can do this in a similar way as with indexers - with set of types for parameters that indicates clearly, which one it should be. They are called automatically on first use of type and we do not need to do that ourselves and we certainly do not need delegates for that.Īny type can have any number of constructors, so the first thing we need is a way to indicate which one we need to call inside a delegate. Yes the static ones also exists, but they are special way of initialization of types not objects. Of course we are talking about non-static constructors. Now it is time to cover following members.įirst members we will discuss are constructors. In previous article and first one in series, we covered: Last article in series you can find here.Ĭode with new features and with bug fixes is available on GitHub and as Nuget package. If you are just looking for a way to create delegates for methods or constructors it is not necessary. If you are not familiar with previous article and looking for detailed explanation of how delegates works, how they are created and why, I strongly encourage you to read it first. This is second article in series, that explains how to create delegates for public and non-public members of public, non-public or not known in compile-time types.
0 Comments
Leave a Reply. |