2

I want to instantiate this class with reflection:

new DbAccess<ProductMoveRequestView>(dbSet);

I have found ProductMoveRequestView with reflection. I have it as a Type object.

I have tried with Activator, seen through MakeGenericType property on Type, and other variations, but I cannot seem to find a way to instantiate this.


It is like duplicate reference, but, as I said in the comments, I still have to instantiate it with an argument.

Type tableType = propertyInfo.PropertyType.GenericTypeArguments.First();
var dbSet = _context.Set(tableType);
Type[] typeArgs = { tableType };
var makeme = typeof(IRepository<>).MakeGenericType(typeArgs);
var o = Activator.CreateInstance(makeme);

The dbSet variable should be sent in as argument when creating the o.


Solution:(I found later) That was easier than I hoped.

Activator.CreateInstance(makeme, constructor, arguments, here);

Sorry guys :) (Hope this helps for anyone else who can't get this to work :)

MarmiK
  • 5,639
  • 6
  • 40
  • 49
Bjørn
  • 1,138
  • 2
  • 16
  • 47
  • 2
    Please post the code where you've tried to use the Activator as well. – Patrik Jul 10 '15 at 09:26
  • 1
    @CodeCaster Not 100% sure this is a dupe as he is asking about constructor parameters (though it's pretty trivial to make that step) – DavidG Jul 10 '15 at 09:32
  • Please post answer separate, it should not be part of question.. :) – MarmiK Jul 10 '15 at 11:36
  • I would love to do that, but I cannot answer my question, because someone thinks it's a duplicate... to a question that did not answer my question... – Bjørn Jul 12 '15 at 08:06

0 Answers0