3

I am looking at HasFlag implementation, there is InternalHasFlag(...) call. How can I see sources of it?

Question tittle might be a bit misleading (feel free to correct).

My original problem comes from attempt to make enum extension method, here is one

public static Enum Mask(this Enum @this, Enum mask)
{
    if (mask == null)
        throw new ArgumentNullException("mask");
    if (!@this.GetType().IsEquivalentTo(mask.GetType()))
        throw new ArgumentException("Type mismatch", "mask");
    return (Enum)(object)((int)(object)@this & (int)(object)mask);
}

I am very unsure in last line and wanted to see how ms-guy does it.

Daniel
  • 9,491
  • 12
  • 50
  • 66
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • 1
    [Related](http://stackoverflow.com/questions/1011702/how-does-the-methodimplattribute-work-in-net), as it's applied to the `InternalHasFlag` method - ie it's an implementation detail of the CLR. You may be able to find more over on the [recent open source releases](https://github.com/dotnet/core) – James Thorpe Jul 20 '15 at 09:39
  • The source cod of .NET is available [online](http://referencesource.microsoft.com/). [InternalHasFlag](http://referencesource.microsoft.com/#mscorlib/system/enum.cs,7a5a597a638c2fdf) though isn't a .NET method, it's part of the runtime itself – Panagiotis Kanavos Jul 20 '15 at 09:42
  • 3
    @PanagiotisKanavos: The OP links HasFlag in the reference source and is specifically asking how to find out how `InternalHasFlag` works because it isn't visible there (to my reading of the question at least). – Chris Jul 20 '15 at 09:43
  • 3
    Asking for external resources, such as .NET Framework or CLR source code if off-topic for Stack Overflow. Is your underlying question _"Is this implementation correct?"_? See [Reimplementing Enum.HasFlag for use in Mono 2.6](http://stackoverflow.com/questions/24278588/reimplementing-enum-hasflag-for-use-in-mono-2-6), [.NET's Enum.HasFlag and performance costs](http://www.codeproject.com/Tips/441086/NETs-Enum-HasFlag-and-performance-costs). – CodeCaster Jul 20 '15 at 09:44
  • @CodeCaster, if you want to get sources of *external* method (*internal* for .net?), then how you do it? Perhaps by disassembling? I am not asking for link (thus not an offtopic I think), nor I am trying to re-implement `HasFlag` or having problem with it. I just posted original problem, because maybe someone can give me answer right away and I don't need `InternalHasFlag` sources anymore. – Sinatr Jul 20 '15 at 09:57
  • Read your question. _"I am very unsure in last line"_ is not a proper problem description. Remove the parts about the .NET CLR source code (which _is_ off-topic - and the CLR is written in C++ and hard to decompile, you could take a look at Mono and see how they do it) and ask a proper question about how to implement your Mask method. – CodeCaster Jul 20 '15 at 09:58
  • @CodeCaster, thanks for [codeproject](http://www.codeproject.com/Tips/441086/NETs-Enum-HasFlag-and-performance-costs) link, this should be the answer tbh. They use `ulong` (and I was using `int`) as well as some methods instead of casting. – Sinatr Jul 20 '15 at 10:00
  • 2
    FWIW, I think [this](https://github.com/dotnet/coreclr/blob/master/src/vm/reflectioninvocation.cpp#L3707) is the implementation. As you can see, it's no longer .NET as it's part of the CLR that's running your .NET code. I found it using [a simple google search](https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:github.com+internalhasflag&filter=0) – James Thorpe Jul 20 '15 at 10:00
  • @JamesThorpe, thanks. Haven't thought to use google to look for that, lol. But if I would, my question would be different, because I've no idea what is going on in that `FCIMPL2` =) – Sinatr Jul 20 '15 at 10:04
  • I'm struggling to see the point of your extension method. What ultimately are you trying to achieve? – Stephen Kennedy Jul 20 '15 at 10:27
  • @StephenKennedy, bits [masking](http://stackoverflow.com/q/10493411/1997232). – Sinatr Jul 20 '15 at 11:06

0 Answers0