| |

VerySource

 Forgot password?
 Register
Search
View: 618|Reply: 5

Separating interface and implementation in .net components

[Copy link]

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-10 13:30:01
| Show all posts |Read mode
Detailed description of the problem:
I define an interface IFoo, and then the TFoo class implements the IFoo interface. IFoo and TFoo are in two different files, and these two files are in different DLLs.

Now I have a module A that uses the services provided by TFoo through IFoo. How can module A only reference IFoo and not TFoo? (The key is to activate the interface defined by IFoo in A without Directly refer to the TFoo class, just like in the COM component, the A module can use the interface file provided by the COM component, and then the COM interface can be activated through a CLASSGUID and CreateComObject method.
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-6-3 17:00:02
| Show all posts
.NET can also be a COM component
or
With reflection
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-6-5 22:45:01
| Show all posts
reflection

Assembly assembly = Assembly.LoadFrom(TFoo.dll);
Type type = assembly.GetType("TFoo");
object instance = Activator.CreateInstance(type,parameters,null);
IFoo foo = (IFoo)instance;
Reply

Use magic Report

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-6-7 18:15:01
| Show all posts
zhenjbd:
Thank you! Somewhat understood, whether assembly is also used for dynamically loading DLL in c#. Can you give me a detailed example?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Invalid IP Address

Post time: 2020-6-8 13:45:01
| Show all posts
interface
namespace InterFace
{
public interface Example
{
void Show();
}
}

class
namespace InheritClass
{
/// <summary>
///
/// </summary>
public class InheritExample: InterFace.Example
{
public InheritExample()
{
//
// TODO: コンストラクタ ロジックをここにaddition してください.
//
}

public void Show()
{
}
}
}


Specific call
Assembly assembly1 = Assembly.LoadFrom (@"F:\201701\InheritClass\bin\Debug\InheritClass.dll");

InterFace.Example object1 = (InterFace.Example)assembly1.CreateInstance("InheritClass.InheritExample", true);

object1.Show();
Reply

Use magic Report

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-6-9 16:30:01
| Show all posts
thank you all! Ask again:
Can Assembly be configured? I see that there are two strings used in the above example, each in C# project has a
AssemblyInfo.cs, is it possible to realize that part of the code is configurable?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list