Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnityContainer and internal constructor

I have a class with internal constructor and want to Resolve it from Unity (2.0).

public class MyClass {
    internal MyClass(IService service) {
    }
}

then I'm doing

_container.Resolve<MyClass>();

when I do so I have an exception

Exception is: InvalidOperationException - The type MyClass cannot be constructed. 

IService is registered and the only problem is that constructor is internal. I really want this class to be public, but I want it to be creatable only via a factory (in which I'm actually calling container.Resolve<MyClass>()).

Is there a way to make Unity see that internal constructor? Like InternalsVisibleTo or something?

like image 380
Shaddix Avatar asked Jun 20 '11 13:06

Shaddix


People also ask

Can a constructor be internal?

A constructor can be either internal or public, an internal constructor marks contract as abstract.

What is Unitycontainer C#?

The Unity Container (Unity) is a full featured, extensible dependency injection container. It facilitates building loosely coupled applications and provides developers with the following advantages: Simplified object creation, especially for hierarchical object structures and dependencies.

What is Injectionconstructor?

A class that holds the collection of information for a constructor, so that the container can be configured to call this constructor.


1 Answers

Just make the class internal and the constructor public...

  1. Interface public
  2. Class internal
  3. Constructor of class public.
like image 56
Razer Avatar answered Oct 24 '22 18:10

Razer