By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

MC++ Reflection

P: n/a
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,
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

--------------------
From: "Lev" <bo*****@hotmail.com>
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

Nov 17 '05 #2

P: n/a
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 [VCPP MSFT]" <ar****@online.microsoft.com> wrote in message news:Q$**************@cpmsftngxa10.phx.gbl...

--------------------
From: "Lev" <bo*****@hotmail.com>
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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.