Set the reference type (class) without the need for an additional link

I have a multi-level .dll system between my application, where the lowest level has a class that provides certain functionality - an instance of this class can be obtained through the GetClass () function, and then I can access its properties (basically, collecting information about changing objects).

Now I noticed that when I want to access this information from the next higher level .dll, the compiler complains that I have no link to the lower level .dll (the one that defines the class) - in fact, I would like to avoid to have a good layered structure in my architecture.

How to get around this? Is it possible to re-view the reference type? Should I really write my own shell if I want the exact same functionality? Or do I even need to reference the low-level DLL again?

(If this helps:

dll1: class myClass, myClass GetMyClass()
dll2: myClass GetMyClass()
exe: how to access result from calling GetMyClass (dll2) without referencing dll1?

)

+5
source share
6 answers

, myClass dll1, myClass . , GetMyClass() dll2, - dll2. , myClass ( , AutoMapper, ), dll2. - :

// dll1
class myClass
{
    ...
}

myClass GetMyClass()
{
    ...
}

// dll2
class myClass2
{
    public myClass2(myClass c)
    {
        // instantiate myClass2 with values from myClass
    }
}

myClass2 GetMyClass()
{
    // somehow get myClass and convert it to myClass2 here
}
+1

, , dll, DLL .

, () . .

// common dll
public interface IMyClass
{
    string MyData { get; set; }
    IMyClass GetMyClass();
}

// dll1
public class myClass : IMyClass
{
    public string MyData { get; set; }
    public IMyClass GetMyClass() { return new myClass() { MyData = "abc" }; }
}

// dll2
public class myClass2
{
    public IMyClass GetMyClass()
    {
         var c1 = new myClass();
         var c2 = c1.GetMyClass();
         return c2;
    }
}

// exe (references common and dll2)
public class Program
{
    public static void Main(string[] args)
    {
        var c1 = new myClass2();
        IMyClass c2 = c1.GetMyClass();
        Console.Writeline(c2.MyData);
    }
}
+3

- . , , , DLL, .

, :

Assembly.LoadFrom(path); //get the assembly as a local object
Activator.CreateInstance(type); //create an instance of a type
Assembly.GetType(string);//fetch a type by name from the assembly

, , .

:

asm = Assembly.LoadFrom(Path.Combine(Environment.CurrentDirectory, filePath));

Type[] types = asm.GetTypes();
for (var x = 0; x < types.Length; x++)
{
    var interfaces = types[x].GetInterfaces();
    for (var y = 0; y < interfaces.Length; y++)
    {
        if (interfaces[y].Name.Equals("MyTypeName"))
        {
            isValidmod = true;
            var p = (IMyType)Activator.CreateInstance(types[x]);
                            //Other stuff
            }
    }

: . , , , .NET 1.1, : . , .

, , 50 , , . , .

+2

DLL1, , . , , dll exe. GetMyClass() DLL1, exe, dll1 .

+1

4- DLL, . .

, :

// DLL1
class ClassInDLL1 : IClassInDLL1
{
}

// DLL2
class ClassInDLL2
{
    public IClassInDLL1 GetClassInDLL1()
    {
        return new ClassInDLL1();
    }
}

// DLL3
class ClassInDLL3
{
    public void DoSomething()
    {
        var dll2 = new ClassInDLL2();
        var dll1 = dll2.GetClassInDLL1(); // dll1 variable is of type IClassInDLL1

        // do stuff with dll1
    }
}

// interface DLL
interface IClassInDLL1
{
}

I will be honest, but this architecture like this is usually not an amazing idea if your project is really big. I believe that artificially creating assembly assemblies like this well in advance can cause you unnecessary pain, not to mention the fact that you get 3-4 assemblies for a medium or small project that may require only 1.

+1
source

I am going with Nick using any Ioc framework like spring.net or Microsoft. for the correct implementation of the idea http://martinfowler.com/articles/injection.html

+1
source

All Articles