Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi :)

I'm developing a simple compiler for .Net, in C++, but am having problems with my include system, therefore not seem to find a way to recognize what kind of method I'm calling from dll if it is void , string, int...
so, my include system call the .net dll call

@include System
@include System:Windows:Forms


when will generate the code, need to know, what kind of method...
I wonder if anyone knows a way to get the type of methods, without using tools .net

One way I thought of using it: Create an application in C# or another .net language to generate a TXT, and this txt, contain the name of the methods and their proper type, and at compile time, I carry this txt and caught at compile time the type of method

But, I whant to know, if have an another way.

Thanks.
Alexandre

>----------------------------------------------

About this program that I talked is that:


C++
using namespace System;
using namespace System::IO;
using namespace System::Reflection;

ref class Reflector
{
public:
    void LoadAndReflect(String^ assemblyFileName)
    {
        Assembly^ assembly = Assembly::Load(assemblyFileName);
        array<Type^>^ types = assembly->GetTypes();
        StreamWriter^ sw = gcnew StreamWriter("mscorlib.txt");

        for each(Type^ t in types)
        {
            array<MethodInfo^>^ methods = t->GetMethods();

            for each(MethodInfo^ method in methods)
            {
                String^ base = method->ReturnType->ToString();
                array<String^>^ auxBase = base->Split('.');
                if(auxBase->Length > 1)
                    sw->WriteLine(String::Format("{0} <::> {1}", auxBase[1], method->Name));
                else
                    sw->WriteLine(String::Format("{0} <::> {1}", auxBase[0], method->Name));
            }
        }
        sw->Close();
    }
};

int main(array<System::String ^> ^args)
{
    Reflector^ ref = gcnew Reflector();

    Console::WriteLine("Reflection on {0}", "mscorlib.dll");
    ref->LoadAndReflect("mscorlib.dll");
    return 0;
}


And, in compiler time, I open that txt and I get the method type
....


>---------------------------------- Edit 12:14 ( BRT )

I know ( now ) about the IMetaDataAssemblyImport, but, I don't know, how to get the informations usint that class :(
Posted
Comments
Sergey Alexandrovich Kryukov 21-Nov-12 14:06pm    
My 5 for the question; more the for whole topic OP is going for; the question itself may seem somewhat naive, nevertheless, interesting enough... :-)
--SA
Alexandre Bencz 21-Nov-12 17:28pm    
Oh, thanks :)
[...... sensitive info removed ...... -- SA]
:)
Sergey Alexandrovich Kryukov 21-Nov-12 17:43pm    
You can contact me through my Web site known from my profile page, see "Contact me". If it does not work (I recently had trouble with hosting providers), please comment on any my post again.

We could talk. I reviewed your links. Your work may be very interesting, but without some description of your goals and appropriate architectural and development cycle overview, your code does not tell me much.

I've developed the whole successful code generation-based technology, metadata-driven. It was done before OMG Metadata Warehouse architecture was published, was way more lightweight and resembled OMG MEF (Meta-Object facility). However, later on, I concentrated on .NET-only (more exactly, CLI, also with Mono) and found it more productive to develop metadata-driven technology based on metadata coded manually in the form of .NET classes. Typically, these classes plays the role of (meta)metadata, meta-square-data (2nd degree of meta). This way, code generation in its traditional approach is excluded. You can use Reflection based on those .NET declaration and generate the whole UI, persistence (think Data Contract) and a lot more. I actually use this approach in applications, in the application fields where such degree of flexibility is really critical. Instead of traditional code generation, in certain parts, System.Reflection.Emit is used. The code is emitted, not generated. Sounds interesting?

Now I work, among other things, at alternative Data Contract persistence. In this field, persistence driven by just Reflection is pretty slow, so it can too slow for some applications, so I use System.Reflection.Emit. It it somewhat tedious work, but when the result is highly universal (and Data Contract technology is extremely universal, data-agnostic), it worth it.

--SA
Alexandre Bencz 21-Nov-12 17:52pm    
Oh, that's true :)
so, I have developed a intresting lib in C#, to generate an native EXE :)
https://github.com/bencz/CoffLib
Sergey Alexandrovich Kryukov 21-Nov-12 18:26pm    
Any documentation, even short?
--SA

1 solution

You cannot use System::Reflection to dig into unmanaged code by one simple reason: it does not have metadata used by Reflection.

If you understand how .NET works, you can see that the compiler (and the IDE) can "see" all the metadata of referenced assemblies and can ensure correct linkage of all the "external" types and their members during compilation. In C++, there is no such things at all. Legacy languages was created oriented to single-module programming, and modular programming was introduced based on archaic bad dirty #include (header files) and object files.

In fact, many programming systems working with unmanaged platforms, had metadata similar to the .NET metadata many years ago: Ada, Turbo Pascal, Object Pascal, Delphi, Modula, and a lot more. The major difference with .NET is: that old metadata was totally implementation-depended (Ada programming systems by different manufacturers had incompatible metadata structures and formats), and, importantly, metadata was not included in executable modules, as it is done with NET assemblies. The metadata of those systems worked pretty much line in .NET, but it had to be supplied separately, as with C++ object files or libraries; that metadata could be considered as extension of object-file system, not executable modules.

If you look thoroughly at C++, C++/CLI mixed mode (managed + unmanaged) development and even at P/Invoke, you can easily figure out that metadata information is passed either through actual source code (mixed-mode case) or "manually", by writing method signatures of method based on some preexisting knowledge. Why do you think? Because the lack of metadata. And this is the exact reason why it's not possible to make Reflection capable of "seeing" the declaration of unmanaged code.

—SA
 
Share this answer
 
v7
Comments
Alexandre Bencz 21-Nov-12 13:37pm    
Oh, Thanks Sergey Alexandrovich Kryukov ( beautiful name ) :)
btw... https://gist.github.com/4116247
look that link, in this link, I have puted the SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE OF SAMPLE.... of my experimental language :)
So, for example, If i want to use the MessageBox ( of .net ) I need to know the return type, so, to get this information, to use in codegen, I need to get from mscorlib.dll ( system.windows.forms .... btw), and, when I see the Ada .net, I see one project inside the ada code, to generate an "header" with .net methods information ( see my question...: http://stackoverflow.com/questions/8141727/using-net-commands-in-ada )

So, one guy in StackOverFlow, recomended to use the: Shared Source Common Language Infrastructure 2.0
And see the file: csharp/sscomp/import.cpp :)
So, let's go to study more and more :)
SO, THANKS FOR YOUR "SOLUTION", very very intresting :) and good :)
Thanks a lot :)
Sergey Alexandrovich Kryukov 21-Nov-12 13:47pm    
You are very welcome.
Thank you for accepting the answer (could not be a solution, but does not have to be), and for the links. I'll take a look. If I have interesting notes, I'll write you my feedback, let me see.

Good luck, call again.
--SA
Sergey Alexandrovich Kryukov 22-Nov-12 11:43am    
OK, now I can answer your question in StackOverflow:
http://stackoverflow.com/questions/8141727/using-net-commands-in-ada

Please do the following:
1) ask a new question at CodeProject;
2) notify me by adding your comment to this comment;
3) in this comment, give me a link to your new question.

OK?

(Quality of .NET binding is really frustrating, but I managed to put .NET Ada 2012 application to work. For a comprehensive work-around might be needed: an application to automate the build of data binding packages (the company effort is ridiculous); the projects can be done custom to add this step. Additional problem is: interference of the .NET framework installation of versions above 2.0 with the v.2.0, required for the GNAT bindings. For now, I resolved it manually.)

--SA
Alexandre Bencz 22-Nov-12 11:54am    
Oh, ok :)
so, i'm only on Skype :)

http://www.codeproject.com/Questions/497711/Usingplus-netpluscommandsplusinplusAda

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900