MC++ Reflection

L

Lev

Hi,

I have some code that does reflection on an assembly I load. When I try to
get the attributes on one of the methods implemented in the assembly, the
MC++ version does not return anything. See code below:

Object __gc* memberAttributes[] =
info[MemberIndex]->GetCustomAttributes(__typeof(EntryPointAttribute),
false);

info is declared as follows: MemberInfo* info __gc[], and is populated using
GetMembers on the assembly.

However, when I do the following from C#, it all works well:

object[] memberAttributes =
mi.GetCustomAttributes(typeof(EntryPointAttribute), false);

mi is also a MemberInfo

The problem is that using the MC++, an empty array is returned to me
(needless to say, both the MC++ and the C# is run on the same assembly).

Thanks,
 
A

Arjun Bijanki [VCPP MSFT]

--------------------
From: "Lev" <[email protected]>
Subject: MC++ Reflection

Hi,

I have some code that does reflection on an assembly I load. When I try to
get the attributes on one of the methods implemented in the assembly, the
MC++ version does not return anything. See code below:

Object __gc* memberAttributes[] =
info[MemberIndex]->GetCustomAttributes(__typeof(EntryPointAttribute),
false);

info is declared as follows: MemberInfo* info __gc[], and is populated using
GetMembers on the assembly.

However, when I do the following from C#, it all works well:

object[] memberAttributes =
mi.GetCustomAttributes(typeof(EntryPointAttribute), false);

mi is also a MemberInfo

The problem is that using the MC++, an empty array is returned to me
(needless to say, both the MC++ and the C# is run on the same assembly).

Thanks,

Nothing jumps out at me; this should work. Can you post a more complete
sample, or code that reproduces the problem?
 
L

Lev

// Do some reflection jazz to get the proper type
Type* ExportedTypes __gc[] = m_pAssemblyToExecute->GetExportedTypes();
for(int Index = 0; Index < ExportedTypes->Count; ++Index) {
MemberInfo* info __gc[] = ExportedTypes[Index]->GetMembers(static_cast<BindingFlags>(BindingFlags::public | BindingFlags::Instance | BindingFlags::DeclaredOnly));
for(int MemberIndex = 0; MemberIndex < info->Count; MemberIndex++) {
// It is the following line that doesn't seem to work in MC++: At least the MethodAttributes array has length 0
Object __gc* MethodAttributes[] = info[MemberIndex]->GetCustomAttributes(__typeof(EntryPointAttribute), false);
int AttributeCount = MethodAttributes->Length;
if(0 <= AttributeCount) {
for(int AttributeIndex = 0; AttributeIndex < AttributeCount; AttributeIndex++) {
// TODO
// Would like to be able to do something along the following:
// if(MethodAttributes[AttributeIndex]->GetType() == __typeof(EntryPointAttribute)->FullName) { ... }
// But this may be related to the usage of __typeof above, because this check does not work

if(0 < AttributeCount) {
Object __gc* target = Activator::CreateInstance(ExportedTypes[Index]);
Object* RuntimeArgs __gc[] = {__box(0)};
Object __gc* Result = ExportedTypes[Index]->InvokeMember(info[MemberIndex]->Name,
static_cast<BindingFlags>(BindingFlags::Default | BindingFlags::InvokeMethod),
0,
target,
RuntimeArgs);
}
....

The assembly I am reflecting over is defined as follows:
using RuntimeBridge;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class MyClass : AxaptaBase {
public MyClass() {
Debug.WriteLine("MyClass ctor");
}
[EntryPoint()]
public void Main(Object args) {
try {
StreamWriter sw = new StreamWriter(@"C:\RuntimeBridgeTest.txt");

string ReturnVal = Lookup.TableIdToName(1916);

sw.Write(TableName);
sw.Close();
}
catch(Exception ex) {
}
}

In the C# class above, Lookup is defined in the assembly that does the reflection (so I have a reference back to the assembly that did the reflection and instantiation). When I try to create an instance of the C# class, my MC++ version gets a TargetInvocationException, whereas the C# version, that does exactly the same, does not.

The part to note, for now at least :), is the fact that the line:
Object __gc* MethodAttributes[] = info[MemberIndex]->GetCustomAttributes(__typeof(EntryPointAttribute), false);
returns an empty array in MC++ where as the line
object[] memberAttributes = mi.GetCustomAttributes(typeof(EntryPointAttribute), false);
in C# works perfectly.


Arjun Bijanki said:
--------------------
From: "Lev" <[email protected]>
Subject: MC++ Reflection

Hi,

I have some code that does reflection on an assembly I load. When I try to
get the attributes on one of the methods implemented in the assembly, the
MC++ version does not return anything. See code below:

Object __gc* memberAttributes[] =
info[MemberIndex]->GetCustomAttributes(__typeof(EntryPointAttribute),
false);

info is declared as follows: MemberInfo* info __gc[], and is populated using
GetMembers on the assembly.

However, when I do the following from C#, it all works well:

object[] memberAttributes =
mi.GetCustomAttributes(typeof(EntryPointAttribute), false);

mi is also a MemberInfo

The problem is that using the MC++, an empty array is returned to me
(needless to say, both the MC++ and the C# is run on the same assembly).

Thanks,

Nothing jumps out at me; this should work. Can you post a more complete
sample, or code that reproduces the problem?

--
Arjun Bijanki, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top