开发者

F# type extensions in C# project: System.Runtime.CompilerServices.Extension missed?

I'm diving into F# and it's very fascinating. I'm trying to marry Optional Types and C# like here. Pretty interesting thing... however I miss something important I guess:

#light
namespace MyFSharp

// C# way
[<System.Runtime.CompilerServices.开发者_运维知识库Extension>]
module ExtensionMethods =
    [<System.Runtime.CompilerServices.Extension>]
    let Great(s : System.String) = "Great"


using System;
using MyFSharp;  // reference the F# dll
class Program
{
    static void Main(string[] args)
    {
        var s = "foo";
        //s.Awesome(); // no
        Console.WriteLine(s.Great());  // yes
    }
}

That's very simple - and I guess it's too early for me or something... I get:

The type 'Extension' is not defined 

Maybe it's too early... but I just don't get why it isn't found.

Thanks, Marius


Does your F# project have a reference to the System.Core assembly?

EDIT: Hmm... perhaps explicitly call it ExtensionAttribute instead of just Extension? Maybe if the namespace is specified (instead of just a simple name) it doesn't try with the Attribute suffix?


Extension methods need to be static in a static class. Does F# actually emit it as such? If not, then you are out of luck.

Update

I just tried it, and it appears to emit static class and method. I got the same error as you, but fixed by compiling as:

fsc -r System.Core test.fs

Then looking at the output of the assembly in Reflector, it looks all good and should work.


Sounds like your F# project is targeting the .Net Framework 2.0.

Try changing it to .Net Framework 3.5 (under the project properties -> Application).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜