You need to specify the name of the assembly as well:

1
let ty = Type.GetType(@"Microsoft.FSharp.Tuple<System.Int, System.Int>, FSharp.Core")

Why? GeType(string) has strange behavior, if the type is defined in the current executing assembly or in mscorlib then only the name is needed, otherwise you need the assembly name too. I've been caught out with this one myself. See msdn for further details:
[link:msdn.microsoft.com]

Rob

By on 8/25/2008 6:07 AM ()

Thanks Rob! It seems that this works fine for, say, unit:

let unit_type = System.Type.GetType(@"Microsoft.FSharp.Core.Unit,FSharp.Core");;

But, trying the following:

let tuple_type = System.Type.GetType(@"Microsoft.FSharp.Core.Tuple<System.Int32,System.Int32>,FSharp.Core");;

gives

System.IO.FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

at System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)

at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)

at System.RuntimeType.PrivateGetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)

at System.Type.GetType(String typeName)

Any idea why this could be?

By on 8/25/2008 6:53 AM ()

Yep, the CLR use a slightly differnt syntax for generic type names than C#, so what you actually need is:
Microsoft.FSharp.Core.Tuple`2[System.Int32, System.Int32]

Sorry forgot about this in the previous post. I think is because in IL you can use <> in type names.

Cheers,
Rob

By on 8/25/2008 7:11 AM ()

Wonderful, thanks Rob!

By on 8/25/2008 7:13 AM ()

FFR, I found a better way to do this while browsing some of the Linq samples:

let tuple2 t1 t2 =

((typeof<int * int>).GetGenericTypeDefinition()).MakeGenericType([|t1; t2|])

By on 8/25/2008 8:13 AM ()

Just to mention that the F# CTP release (which will be out soon) will have a method

1
2
3
4
 

   Reflection.FSharpType.MakeTupleType

that can be used to solve this kind of problem for arbitrary sized tuple types. e.g.

1
2
3
4
 

Reflection.FSharpType.MakeTupleType [| typeof<int>; typeof<int> |]

There are also matching destructors and discriminator

1
2
3
4
5
6
 

   Reflection.FSharpType.GetTupleElements

   Reflection.FSharpType.IsTuple

Thanks

don

By on 8/28/2008 5:34 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper