Object Creation

Jul 21, 2008 at 5:43 PM
There is most likely a simple answer for this.

I have 2 class.  Class A and B.

IB, B>();

Class A creates a collection of class B's inside it. 

How do I do this without passing in the Unity container object or referencing a singleton Unity container within Object A?


Jul 21, 2008 at 9:20 PM
Would something like the following work for you?   The injection constructor will provide an instance that can be used by the Activator.CreateInstance() method.

    public interface IB
        int Index { get; set; }

    public class A : IA
        private List<IB> bList = new List<IB>();

        public A(IB bInstance)
            for (int i = 0; i < 20; i++ )
                IB bitem = (IB)Activator.CreateInstance(bInstance.GetType());
                bitem.Index = i;

Jul 21, 2008 at 11:32 PM

That is cool.  I did not know you could do that.

I ended up creating a factory interface and setting it with unity. I am building this for some utility classes.

What its for is a class called IDirectoryInfo based on DirectoryInfo and IFileInfo based on FileInfo.  These classes are hard to test and I was making interfaces for them.  I ran into a problem when the IFileInfo interface needed to be constructed in the DirectoryInfo class. 

Now that I look back I think it probably alright to hard code IFileInfo implementation with the corresponding IDirectoryInfo Implementation.