Is it possible for me to do the following?
public static T Merge<T>()
{
object x = Activator.CreateInstance<T>();
//Do some stuff with x
return (T)x;
}
private static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return assembly.GetTypes().Where(
t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal) &
!t.IsInterface).ToArray();
}
public static void Main()
{
Type[] typelist = GetTypesInNamespace(
Assembly.GetExecutingAssembly(), "Myapplication.Web.DomainObjects");
Parallel.ForEach(typelist, type =>
{
var task1 = Task.Factory.StartNew(() => Merge<type>());
// is it possible to do this way? Merge<type> ??
});
}
No, you can’t do this – Generics are used when you know the type in advance at compile time, however you don’t in this case.
I believe that what you really want to do is something a little like this:
Your foreach statement now looks slightly different: