user788454
user788454

Reputation:

.NET: How to build a DLL from source code without the referenced DLL?

Look at the following code of a .NET class library:

public class Test
{
    public void LogTransaction(Transaction t)
    {
         Logger.Log(t.ToString());
    }
}

You can see that class Transaction and Logger are defined in another DLL, which this DLL references.

I need to write some code to build the above source code into a .NET class library. If I don't have the other DLL, is there anyway my code can build this DLL? I know Visual Studio will not build if the other DLL is missing.

Upvotes: 0

Views: 265

Answers (1)

PaulG
PaulG

Reputation: 174

If you can't reference the required assembly at build time, my suggestion is to create a late-binding wrapper class that works via reflection to create the object and invoke the members.

i.e.

public class TransactionWrapper
{
    private object _transaction;

    public TransactionWrapper()
    {
        _transaction = Activator.CreateInstance(Type.GetType("Transaction"));
    }

    public override string ToString()
    {
        var mi = _metadata.GetType().GetMethod("ToString");
        return (string) mi.Invoke(_metadata, null);
    }

    ... // continue with necessary members
}

Obviously this technique introduces a number of challenges, chief of which is no compile time safety for the calling convention.

Upvotes: 0

Related Questions