public void Activate() { object obj = CustomActivator.CreateInstance(_member.DeclaringType); MethodInfo method = obj.GetType().GetMethod(_member.Name); _cachedInstance = Delegate.CreateDelegate(_delType, obj, method); }
public override void Activate() { IDictionary <string, Lazy <object> > valuesLazy = ImportValues.ToDictionary( x => x.Key.MemberName, v => v.Value); if (CachedInstance == null && this.Definition.ParameterlessConstructor) { _cachedInstance = CustomActivator.CreateInstance(Definition.GetPartType()); var unpackValues = valuesLazy.ToDictionary(k => k.Key, v => v.Value.Value); CustomActivator.PopulateObjectProperties(_cachedInstance, unpackValues); } else if (this.Definition.ParameterlessConstructor) { var unpackValues = valuesLazy.ToDictionary(k => k.Key, v => v.Value.Value); CustomActivator.PopulateObjectProperties(_cachedInstance, unpackValues); } else { var constructor = Definition.GetConstructor(); var requiredParameters = constructor.GetParameters(); //Sorting imports to match constructor order object[] unpackValues = new SortedDictionary <string, Lazy <object> >(valuesLazy) .Zip(requiredParameters, (lazyValues, required) => new { lazyValues, required }) .OrderBy(x => x.required.Name) .Select(x => x.lazyValues.Value.Value) .ToArray <object>(); _cachedInstance = Definition.GetConstructor().Invoke(unpackValues); } }
public void CustomActivatorMustInstantiateIntarfaceWithItsDefaultValues() { var resultObject = CustomActivator.CreateInstance <IParametersTest>(); resultObject.Integer.Should().Be(0); resultObject.Boolean.Should().Be(false); resultObject.AnotherInteger.Should().Be(0); resultObject.AnotherBoolean.Should().Be(false); resultObject.IntegerNullable.Should().Be(null); }