4 minute read

If you don't know what is CInject, I would recommend you to read this article and get the download latest version of CInject from CodePlex website.

Creating a Basic Injector

Once you have the latest executable, you can follow through these steps to create your own injector

Create a new Visual Studio Class Library Project (C# or VB.NET) and add a Reference to CInject.Injections.dll.  You will find this assembly with the CInject application

CInject-InjectorReference

Add a class called 'MyInjector' that derives from ICInject interface.  This interface is part of CInject.Injections reference

The MyInjector class would look like

 

using CInject.Injections.Interfaces;

namespace InjectorLib
{
    public class MyInjector : ICInject
    {
        public void OnComplete()
        {
            // This is called before exit of injected method
        }

        public void OnInvoke(CInject.Injections.Library.CInjection injection)
        {
            // Called at the entry of injected method

            // To get the arguments passed to injected method
            var arguments = injection.Arguments;

            // To get value of property Text in any argument
            var propertyValues = injection.GetPropertyValue("Text");
        }

        public void Dispose()
        {
            // dispose here if you want to
        }
    }
}

Compile the Visual Studio Project to create a DLL named MyInjector.dll and follow the steps mentioned in Using Injectors in CInject

Adding Configuration to your Injector

Adding configuration to an injector is similar to adding a configuration to any other project.  You can choose from one of the following ways

  1. Rely on the configuration to be added in the configuration (app.config / web.config) of the target assembly / executable
  2. Add an independent configuration file (say, myconfig.xml) that needs to be copied in the target assembly/executable folder
  3. Add an independent configuration file and retrieve it from a Network Shared drive

I would not recommend Point -1 as it may cause conflicting configurations.  To use step 2, you can use a very handy feature of CInject

Let's say that your configuration file is myinjector-configuration.xml and it is added to the Visual Studio Project

You need to change the property of the configuration file to 'Copy Always' and decorate your injector class with DependentFiles attribute

using CInject.Injections.Attributes;
using CInject.Injections.Interfaces;

namespace InjectorLib
{
    [DependentFiles("myinjector-configuration.xml")]
    public class MyInjector : ICInject
    {
        #region Code

        // Other code here

        #endregion
    }
}

When CInject applies this injector to any assembly, it ensures that it copies myinjector-configuration.xml to root folder of target assembly / executable.

You can access this configuration file in code using standard .NET libraries

CInject-DependentFiles1

What if I some dependent / referenced assemblies as well?

Dependent / Referenced assemblies in your injector work same as configuration files.

You just have to mention them in the DependentFiles attribute and ensure that they are in the same directory as that of the injector.

CInject-DependentAssemblies