Ok. Here it is. Here's a simple test case which demonstrates the problem
I'm having. If I'm doing something wrong, please let me know.
There are 3 projects created. One is the "interface dll" which contains the
abstract class. Another is the "implementa tion dll" which contains a class
that derives from and implements the abstract class. And there's a Windows
forms app to use as the driver. Here are the steps and code.
1. Create a "Class library" project. This will define an abstract class to
act as an interface. Call it "InterfaceD ll".
Here is the code for the source file in this project. Compile it.
using System;
namespace InterfaceDll
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public abstract class TheInterface
{
public abstract int DoSomething1();
public abstract int DoSomething2();
}
}
2. Create another "Class library" project. Call it "Implementation Dll".
Add a project reference to the built "InterfaceD ll" you created in step one.
Here is the source for this one.
using System;
namespace ImplementationD ll
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Implementation : InterfaceDll.Th eInterface
{
public Implementation( )
{
}
public override int DoSomething1()
{
//byte[] myBytes = {0xFF, 0xFF, 0xFF, 0xFF};
//foreach (byte b in myBytes)
//{
// Console.Write(b .ToString() );
//}
return 0;
}
public override int DoSomething2()
{
return 0;
}
}
}
* Note the lines commented out. Leave them commented out for now. Build
this dll.
3. Create a Windows forms app with a single button on the form. Add a
reference to the "InterfaceDll.d ll" built in step 1 to this project. Add a
click handler for the button. Add the following code to the button handler.
string strCurDir = Environment.Cur rentDirectory;
string filename = Path.Combine(st rCurDir, "implementation dll.dll" );
//Load the Assembly
Assembly a = Assembly.LoadFr om(filename);
// get all the types in the loaded assembly
Type[] types = a.GetTypes();
foreach (Type typ in types)
{
// dynamically create or activate(if exist) object
object obj = Activator.Creat eInstance(typ);
}
Build the forms app. Now, copy the "Implementation Dll.dll" from step 2 into
the "\bin\Debug " directory so the code can locate the assembly.
4. Run the windows forms app. Click the button. Note how it runs
properly.
5. Now, uncomment the code in the "DoSomethin g1" method in the
"Implementation Dll" project, build it and (DON'T FORGET) copy the new dll
into the same location as in step 3.
6. Run the windows forms app again. Click the button. It will throw an
exception on the "object obj = Activator.Creat eInstance(typ); " line.
"An unhandled exception of type 'System.Missing MethodException '
occurred in mscorlib.dll
Additional information: No parameterless constructor defined for
this object."
If you change the byte[] array allocation to use "new" instead of an
initializer list like the following, it works ok.
byte[] myBytes = new byte[4];
myBytes[0] = myBytes[1] = myBytes[2] = myBytes[3] = 0xFF;
Why does using an initializer for the byte[] array in the "DoSomethin g1"
method cause an error? Am I doing something wrong?
Thanks!
Terry
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Terry <ch**********@h otmail.com> wrote: I'm a bit puzzled by this. How can all function local code affect the
creation of an object? And why would it make a difference how the array
is created and initialized within a method of the derived class?
Can anyone help clear this up? Am I doing something I shouldn't be?
<snip>
That sounds very odd to me. Could you provide a short but complete
example which demonstrates the problem?
See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too