A
Andy Walldorff
Given the following code....
// FooBar.cs, builds Activate.dll
using System;
public interface IFoo
{
void Bar();
}
public class FooBar : IFoo
{
public FooBar()
{
}
public void Bar()
{
Console.WriteLine( "FooBar" );
}
}
// End of FooBar.cs
// DoActivate.cs, builds DoActivate.exe
using System;
using System.Reflection;
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
IFoo ifoo;
Object obj;
try
{
Assembly a = Assembly.LoadFrom( "Activate.dll" );
Type[] ts = a.GetTypes();
foreach( Type t in ts )
{
if( t.Name == "FooBar" )
{
if( t.GetInterface( "IFoo" ) != null )
{
obj = Activator.CreateInstance( t );
ifoo = obj as IFoo;
if( ifoo == null )
Console.WriteLine("didn't get object" );
else
Console.WriteLine("got object" );
}
else
Console.WriteLine( "Object does not support
IFoo" );
}
}
}
catch( Exception e )
{
Console.WriteLine( "Caught exception {0}", e );
}
}
}
// End of DoActivate.cs
Why is ifoo null? I always get "didn't get object" and yet everything
appears correct, at least to my untrained eye. Thanks in advance!
// FooBar.cs, builds Activate.dll
using System;
public interface IFoo
{
void Bar();
}
public class FooBar : IFoo
{
public FooBar()
{
}
public void Bar()
{
Console.WriteLine( "FooBar" );
}
}
// End of FooBar.cs
// DoActivate.cs, builds DoActivate.exe
using System;
using System.Reflection;
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
IFoo ifoo;
Object obj;
try
{
Assembly a = Assembly.LoadFrom( "Activate.dll" );
Type[] ts = a.GetTypes();
foreach( Type t in ts )
{
if( t.Name == "FooBar" )
{
if( t.GetInterface( "IFoo" ) != null )
{
obj = Activator.CreateInstance( t );
ifoo = obj as IFoo;
if( ifoo == null )
Console.WriteLine("didn't get object" );
else
Console.WriteLine("got object" );
}
else
Console.WriteLine( "Object does not support
IFoo" );
}
}
}
catch( Exception e )
{
Console.WriteLine( "Caught exception {0}", e );
}
}
}
// End of DoActivate.cs
Why is ifoo null? I always get "didn't get object" and yet everything
appears correct, at least to my untrained eye. Thanks in advance!