Interface problem in C#2.0

M

Manuela Abele

Hello everybody,

I have a problem with interface and I need urgent your help.

public interface IKoerper
{
// Properties
int laenge { get; }
int breite { get; }
int hoehe { get; }
int anzahl { get; }
// Methods
double errechneFlaeche();
void hinzufuegen(IKoerper koerper);
void entfernen(IKoerper koerper);
}
----> DLL

public class Quadrat : IKoerper
{
// Variables
private int _laenge;
// // Properties
public int laenge { get { return _laenge; } }
public int hoehe { get { return 0; } }
public int breite { get { return _laenge; } }
public int anzahl { get { return 1; } }

// Methods
public Quadrat(int laenge)
{
_laenge = laenge;
}
public double errechneFlaeche()
{
return laenge * laenge;
}
}

----> DLL

public class Rechteck: IKoerper
{
// Felder
private int _laenge;
private int _breite;
// Eigenschaften
public int laenge { get { return _laenge; } }
public int hoehe { get { return 0; } }
public int breite { get { return _breite; } }
public int anzahl { get { return 1; } }

// Methods
public Rechteck(int laenge, int breite)
{
_laenge = laenge;
_breite = breite;
}

public double errechneFlaeche()
{
return laenge * breite;
}
}

----> DLL


Main program: -> Exe
I want to call the function 'errechneFlaeche'.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the case 1, the first DLL is inside the release folder,
my program calculate the Quadrat
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the case 2, the second DLL is inside the release folder,
my program calculate the Rechteck


I'm not sure, for example I need a virtual class
VirtualClassApp AppCalc = new VirtualClassApp();
double result = AppCalc.errechneFlaeche();

How can I create the class, with a wizard.
Can you gibe me a simple and short instruction. Thanks.

Best regards Manuela
 
A

Andreas Mueller

Manuela said:
Hello everybody,

I have a problem with interface and I need urgent your help.

public interface IKoerper
{
// Properties
int laenge { get; }
int breite { get; }
int hoehe { get; }
int anzahl { get; }
// Methods
double errechneFlaeche();
void hinzufuegen(IKoerper koerper);
void entfernen(IKoerper koerper);
}
----> DLL

public class Quadrat : IKoerper
{
// Variables
private int _laenge;
// // Properties
public int laenge { get { return _laenge; } }
public int hoehe { get { return 0; } }
public int breite { get { return _laenge; } }
public int anzahl { get { return 1; } }

// Methods
public Quadrat(int laenge)
{
_laenge = laenge;
}
public double errechneFlaeche()
{
return laenge * laenge;
}
}

----> DLL

public class Rechteck: IKoerper
{
// Felder
private int _laenge;
private int _breite;
// Eigenschaften
public int laenge { get { return _laenge; } }
public int hoehe { get { return 0; } }
public int breite { get { return _breite; } }
public int anzahl { get { return 1; } }

// Methods
public Rechteck(int laenge, int breite)
{
_laenge = laenge;
_breite = breite;
}

public double errechneFlaeche()
{
return laenge * breite;
}
}

----> DLL


Main program: -> Exe
I want to call the function 'errechneFlaeche'.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the case 1, the first DLL is inside the release folder,
my program calculate the Quadrat
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the case 2, the second DLL is inside the release folder,
my program calculate the Rechteck


I'm not sure, for example I need a virtual class
VirtualClassApp AppCalc = new VirtualClassApp();
double result = AppCalc.errechneFlaeche();

How can I create the class, with a wizard.
Can you gibe me a simple and short instruction. Thanks.

Best regards Manuela

Hi Manuela,

Here is a simple example:

Define the interface in a seperate assembly, e.g. ConsoleApplication.Api

namespace ConsoleApplication.Api
{
public interface IBody
{
double Volume { get; }
}
}

Put the implementations into seperate assemblies, e.g a cube into
ConsoleApplication.Cube.PlugIn:

using ConsoleApplication.Api;
namespace ConsoleApplication.Cube.PlugIn
{
class Cube : IBody
{
private readonly double length;
private readonly double width;
private readonly double height;

public Cube(double length, double width, double height)
{
this.length = length;
this.width = width;
this.height = height;
}
public double Volume
{
get { return width * length * height; }
}
}
}


implement your executing assembly:

using System;
using System.IO;
using System.Reflection;

namespace ConsoleApplication
{
class Program
{
static void Main()
{
string[] plugins =
Directory.GetFiles(
Environment.CurrentDirectory, "*.plugin.dll");

Assembly plugIn = Assembly.LoadFrom(plugins[0]);
foreach(Type possibleType in plugIn.GetTypes())
{
if (!typeof(IBody).IsAssignableFrom(possibleType))
continue;

// instantiate, assuming ctors contract takes
// length, width and height
IBody b =
(IBody) Activator.CreateInstance(
possibleType, 10.0, 10.0, 20.0);

Console.WriteLine(
string.Format("The Body has a volume of {0}!",
b.Volume));
}
}
}
}


Make sure that every assembly is build or copied into a common folder.
As you see, the plugins are loaded by name. You can change this to be
read from a config file.

HTH,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top