3

I am trying to pass the type of a dynamically loaded dll (it uses an interface, but I need the conectrete implementation of this) to a function and am missing something.

var data = LoadAssemblyFromParamenter(pathToDataDll);
Type dataType = data.GetType().MakeGenericType(data.GetType());
SomeTest<dataType>();

public void SomeTest<T>() 
{
    //do something with T 
}

Error is "The Type or Namespace 'dataType' coulnd not be found..."

The concrete type is for a FileHelpers object (that uses fields), so I need the concrete implmentation.

p.s. This has to be .net 3.5 ....

To elaborate SomeMethod<T>( IEnumerable<T> items ) calls

public static void WriteRecords<T>(IEnumerable<T> records, string fileName )
    where T: ICMSDataDictionary
{
    if (records == null || String.IsNullOrEmpty(fileName))
        return;

    if (records.Any())
    {
        FileHelpers.DelimitedFileEngine<T> engine =
            new FileHelpers.DelimitedFileEngine<T>();
        engine.WriteFile(fileName, records);
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
user725819
  • 75
  • 1
  • 6
  • What could the SomeTest method possibly do that accesses the fields without using the interface? – Random832 Sep 26 '11 at 14:06
  • well, ok, I was trying to keep this simple.. Added example. This uses the FileHelpers library. I would like to avoid the ClassBuilder in this case. – user725819 Sep 26 '11 at 14:09

2 Answers2

2

You can not do this, using Method syntax, because, for this you would know the type at compile time. You can, however, invoke your method using reflection.

This code should help you started:

    static void Main(string[] args)
    {
        //Get method Method[T]
        var method = typeof(Program).GetMethod("Method", BindingFlags.NonPublic | BindingFlags.Static);
        //Create generic method with given type (here - int, but you could use any time that you would have at runtime, not only at compile time)
        var genericMethod = method.MakeGenericMethod(typeof(int));
        //Invoke the method
        genericMethod.Invoke(null, null);
    }

    static void Method<T>()
    {
        Console.WriteLine(typeof(T).Name);
    }

With object, it's similar, but you have to construct object dynamically, using reflection.

Marcin Deptuła
  • 11,789
  • 2
  • 33
  • 41
1

Use the "dynamic" keyword:

public void Doit() {
  dynamic data=LoadAssemblyFromParamenter(pathToDataDll);
  SomeTest(data);
}

public void SomeTest<T>(T arg) {
  Debug.WriteLine("typeof(T) is "+typeof(T));
}

!!!EDIT!!!: sorry, I missed that you needed the 3.5 Framework. If so, use this:

public void Doit() {
  var data=LoadAssemblyFromParamenter(pathToDataDll);
  var mi=this.GetType().GetMethod("SomeTest").MakeGenericMethod(data.GetType());
  mi.Invoke(this, new object[0]);
}

public void SomeTest<T>() {
  Debug.WriteLine("typeof(T) is "+typeof(T));
}
Corey Kosak
  • 2,615
  • 17
  • 13
  • 2
    I really wish people would stop suggesting the dynamic keyword to solve problems that can be solved with better design or proper use of reflection. You don't fix type problems by just getting rid of the type system. – siride Sep 26 '11 at 14:20
  • @siride, how is using reflection better than using `dynamic`? The only significant difference is that reflection is much more verbose. – svick Sep 26 '11 at 14:23
  • 1
    I feel a person should do everything humanly possible to stay within the type system, but assuming one really has to escape the type system, you really prefer the reflection API to the dynamic keyword? Why? – Corey Kosak Sep 26 '11 at 14:23
  • @svik: it'd be better to use neither, but reflection doesn't pull in the DLR and the overhead that entails. – siride Sep 26 '11 at 14:24
  • @Corey Kosak: reflection forces you to at least pay lip-service to the type system because, in a way, you are still using it. With dynamic, you really are no longer using the type system at any point (with reflection, if you commit a type error, you will get an exception). If you want to use a dynamically-typed language, use it. They exist and are popular. If you want to use C#, then stick to the static type system. – siride Sep 26 '11 at 14:27
  • Hardly seems worth downvoting my answer. I answered the poster's question in two different ways. The poster was not asking for style or efficiency advice. – Corey Kosak Sep 26 '11 at 14:29
  • @Corey Kosak: I downvoted it back when you just had the dynamic keyword. I'll remove my downvote (edit: I can't -- it says my vote is now locked in until you edit). – siride Sep 26 '11 at 14:33
  • 1
    In my fragment, dynamic is used just long enough to bind SomeTest's type parameter T. Then you're back in a strongly-typed world again. Where's the possibility for a type error here? What possible error will the reflection API catch that the dynamic approach will miss in this case? – Corey Kosak Sep 26 '11 at 14:35