[Mono-list] Generics sharing vs delegates?

Mike Cleaver mike at moga.to
Sun Aug 3 08:25:42 EDT 2008


Hey all,

Congrats on another release,
I've only installed on my mac at the moment (excellent packaging!) and  
have had a go at getting my project to run and for the most part it  
works perfectly.

I have found one issue, which I'm trying to write a unit test for at  
the moment but expect that like other issues I've had (and solved) in  
the past it is probably more due to me doing something wrong:
Running this static method -

		public static T [] GetDirectives<T>(IEnumerable<AbstractDirective>  
directives) where T : AbstractDirective {
			Console.WriteLine("@@@ Orig T is " + typeof(T).Name);
			List<AbstractDirective> matches = new  
List<AbstractDirective>(directives).FindAll(delegate(AbstractDirective  
directive) {
							Console.WriteLine("@@@ T is {0}", typeof(T).Name);
							return directive is T;
						});
			Console.WriteLine("@@@ Final T is " + typeof(T).Name);
			
			return matches.ConvertAll<T>(delegate(AbstractDirective directive)  
{ return directive as T; }).ToArray();
		}

I get the output -
@@@ Orig T is RegisterDirective
@@@ T is AssemblyDirective
@@@ T is AssemblyDirective
@@@ Final T is RegisterDirective

The RegisterDirective and AssemblyDirective classes inherit the same  
abstract class AbstractDirective.  So what we are seeing is that the  
generic type T is different inside the delegate, it is not a type that  
is currently accounted for in the directives enumeration or anything,  
just a different class type.

Can anyone tell me if they can see anything silly? I assume there is a  
better way to select a particular type from a list that I should be  
doing instead?

Mike Cleaver
mike at moga.to




More information about the Mono-list mailing list