|
-
September 17th, 2011, 10:33 AM
#1
Generics conversion problem
I've got an conversion issue with converting an instance to a certain class. This instance is created after I scanned my assembly for a certain type. Don't ask why I'm doing this in complex way. I've got a very good reason for it. Trust me .
I've place three conversions in the code. The third one isn't working but should be correct. Does anybody knows a trick to get the third conversion working too.
Tnx.
@@csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ReflectionTest2
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.GetExecutingAssembly();
// Get all types which implement IElementUpdater
List<Type> typeToChecks = assembly.GetTypes().Where
(t => ((typeof(IElementUpdater).IsAssignableFrom(t)
&& t.IsClass && !t.IsAbstract))).ToList();
foreach (Type typeToCheck in typeToChecks)
{
var instance = MyObjectFactory.CreateObject(typeToCheck);
// Conversion works fine but I'm not able to call the Foo method
IElementUpdater one = instance as IElementUpdater;
// Conversion works fine but is only to proof it is of the correct type
SaleElementUpdateHandler two = instance as SaleElementUpdateHandler;
// Conversie doesn't work
IElementUpdater<IBaseTransactionElement> three = instance as IElementUpdater<IBaseTransactionElement>;
}
}
}
public interface IBaseTransactionElement
{
}
public interface IElementUpdater
{
}
public interface IElementUpdater<T> : IElementUpdater where T : IBaseTransactionElement
{
void Foo(T element);
}
public class SaleElementUpdateHandler : IElementUpdater<FooBar>
{
#region IElementUpdater<FooBar> Members
public void Foo(FooBar element)
{
throw new NotImplementedException();
}
#endregion
}
public class FooBar : IBaseTransactionElement
{
}
}
@@
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|