[Mono-list] Generics sharing vs delegates?

Mike Cleaver mike at moga.to
Mon Aug 4 04:21:27 EDT 2008


After a few seconds thought I realised that this is easier:

public static T [] GetDirectives<T>(IEnumerable<AbstractDirective>  
directives) where T : AbstractDirective {
			List<T> output = new List<T>();
			foreach(AbstractDirective directive in directives) {
				if(directive is T) output.Add(directive as T);
			}
			return output.ToArray();
		}

My app runs fine now as this was the only problem running 2.0p,  
strange that it occurs at all though?

Thanks,
Mike Cleaver

On 03/08/2008, at 8:25 PM, Mike Cleaver wrote:

> 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
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list

Mike Cleaver
mike at moga.to




More information about the Mono-list mailing list