Saturday, June 16, 2012

F# Type Provider as wrapper class II

Thanks to the question from , I can make the F# type provider more useful.  If you have not read the previous post, you can go to here to read that post first. You do not have to put the sealed class in your type provider code. If you can modify the sealed class, it does not make sense to use type provider. This time, we try to reference to a DLL. The source is code is here, in which there are two solutions. One is the type provider project and the other one is the library project contains a seal class. The library will mimic the third party sealed class.

In the source code, the type provider project references to the library1.dll. Everything seems working correctly, until you try to execute the test code in the second Visual Studio,
type T = Samples.ShareInfo.TPTest.TPTestType
let t = T()
let s = t.F2(2)
printfn "AA"
you will get error:
 The type provider 'Samples.FSharp.ShareInfoProvider.CheckedRegexProvider' reported an error in the context of provided type 'Samples.ShareInfo.TPTest.TPTestType', member '.ctor'. The error: Could not load file or assembly 'Library1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

I bet you must jaw-droppingly stare at the screen when you first see this error. Does Visual Studio hide some secrete from you? If you install this DLL into the GAC, everything should be fine. If you do not want to  pollute your GAC, the < visual Studio Installation folder > \Common7\IDE\PrivateAssemblies is the magic folder you can put your Library1.dll.

After the file is under that magic folder, you do not have to restart Visual Studio. Just hit F5 and everything should work. 


If you really want to use the type string as a parameter, there is a tutorial from MSDN. What you need to do is convert the incoming string to type by using Assembly.Load and find the type you want. Please note type provider does not accept System.Type as parameter. It can only take string.



No comments: