1

I have a reference to the type and class from certain assembly.

var type = typeof(SomeNamespace.SomeClass);

Is there a way to get meta about what programming language were used to write this class? C# or VB or F#?

So in result i want something like this

var programmingLanguage = getLanguage(type); // C#

UPD:

Can I analyze what Assemblies or Namespaces where used for this class? Maybe this can be a clue to finding out a language?

UPD2:

So far I could use this ( it's just an example for VB detection )

var referenced = type.Assembly.GetReferencedAssemblies()
    .Where((r) => r.Name.Contains("VisualBasic"));

VB adds Microsoft.VisualBasic. F# also adds it's own assemblies and C++ declares a lot of 'self use' types which I can detect. So this is "a way". I know it's not error prone. And there will be some false cases. So is there anything better?

obenjiro
  • 3,665
  • 7
  • 44
  • 82
  • 8
    Why would this be relevant? – Uwe Keim Sep 24 '13 at 09:47
  • 3
    You seem to be wanting to solve a certain problem by trying to detect the language used to create a certain class (which is not trivial). What is the real problem you're trying to solve? – CodeCaster Sep 24 '13 at 09:51
  • 2
    @UweKeim i can't find legitimate answer :) just consider this as an "interest" – obenjiro Sep 24 '13 at 09:55
  • 2
    @CodeCaster i know it's a stupid but i want to write Unit Test that will ensure that code is written in C#. I know it's stupid, but i personally really interested right now is it even possible or not? – obenjiro Sep 24 '13 at 09:58
  • @Ai_boy I suggest you to check if assembly/type/member is CLS compliant. If so, then only features which are supported by ANY .NET language were used, so you completely should not care about source language – Sergey Berezovskiy Sep 24 '13 at 10:07
  • 1
    -1 as this question makes no sense as a theoretical thought experiment. – Daniel Hilgarth Sep 24 '13 at 10:09
  • @DanielHilgarth well thanks for telling me. But I really have a use case. For unit test. I know it's a stupid, but hey. Why not to try? :) – obenjiro Sep 24 '13 at 10:11
  • @Ai_boy: If it is stupid, why do it? And what use is such a unit test? – Daniel Hilgarth Sep 24 '13 at 10:12
  • @DanielHilgarth are you even a human? :) People usually 'like' to do stupid things :) – obenjiro Sep 24 '13 at 10:13
  • 1
    @Ai_boy: No, I am a robot. I agree that people like to do stupid things, but that usually involves other things that are fun at the same time. Finding out whether an assembly has been originally written in C# or VB.NET doesn't sound like fun ;-) You say you have a use case, but the unit test you mention isn't one. Do you have a real use case or is this really purely a thought experiment? I am asking, because a real use case would give a context that would allow better answers. – Daniel Hilgarth Sep 24 '13 at 10:15
  • 1
    @DanielHilgarth I swear I was asked by higher-ups to write a Unit test that will ensure that code is written in C#. I know that it's 'stupid' so i told them about that. But at the same time I immediately got interested in this. So right now i'm asking about that. I don't like to say that something is not possible if IT IS! So i want to find out? don't u? :) – obenjiro Sep 24 '13 at 10:21
  • You plan on testing VB or C# more ? ;-) Why not just give the assemblies names that remove the guess work. – phil soady Sep 24 '13 at 10:47
  • @philsoady VB, C#, F# and C++ ( they all have differences in assembly - so i can analyze them ) if you know any better way, you welcomed :) – obenjiro Sep 24 '13 at 10:53

3 Answers3

9

.NET does not make this distinction, there's not really a good reason for you to either.

The only way would be to analyse the types and code patterns used. For example, VB projects often reference a different set of assemblies to C#.

If you need to do this for your own assemblies, I would suggest creating a custom Attribute class and decorating your assemblies with it.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
  • Is there any way to find differences. At least in C# or VB ? – obenjiro Sep 24 '13 at 09:54
  • 1
    See also http://www.codeproject.com/Articles/12585/The-NET-File-Format for the information contained in a .NET assembly. – C.Evenhuis Sep 24 '13 at 09:55
  • 1
    For detecting VB.Net assemblies, you could check if the `My` namespace and its classes were generated. – sloth Sep 24 '13 at 10:14
  • Well. I think I will go with analyzing set of referenced assemblies. It's not a good way to do code detection. But at least it's something. – obenjiro Sep 24 '13 at 10:38
5

Take a read of Decompiled assembly, which programming language was used. There is no facility in .NET assemblies to record the source language. In addition, not only are there C#, F# and VB.NET compilers for .NET, but there are many other compilers too (see http://en.wikipedia.org/wiki/List_of_CLI_languages for a taste of the number). Finally, the IL might have been written directly, rather than via a compiler.

You could try to search for patterns in the IL to try and work out the source language, but if would be highly error-prone. I'm not sure what benefit it would offer either.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131
  • is there any way i cant analize IL code of a given type? maybe you know some C# patterns? I'm interested in detecting C# code.. – obenjiro Sep 24 '13 at 10:01
  • 1
    One possible way might be by looking at the case for classes, methods and properties within the assembly. The conventions between C# and VB.Net for example are different. It would only be reliable if conventions are strictly adhered to of course. – David Arno Sep 24 '13 at 10:04
  • Ok, I will try to dig into that. I will tell you as soon as i find anything. – obenjiro Sep 24 '13 at 10:06
  • 2
    As your question shows, you're not following the `C#` casing conventions: `var programmingLanguage = getLanguage(type); // C#`. I don't see how you'll be able to sort the language by looking at the conventions if you're not following them. – Stephane Delcroix Sep 24 '13 at 10:44
1

See Kieren's answer. An alternative is to compare the assembly files, with some id in the name or metadata to define its language.

See How to get the assembly (System.Reflection.Assembly) for a given type in .Net?

This would only be helpful if you're compiling yourself, or the different language'd binaries are named differently or you can manually name them differently.

If you're compiling yourself, you can add metadata to the binary as a post-build event. See Read/Write 'Extended' file properties (C#) for details on how to add. In the build event, you'll be able to access information about the project - such as it's compilation language.

Community
  • 1
  • 1
Jono
  • 3,949
  • 4
  • 28
  • 48