Couple Reflection Questions

S

sippyuconn

Hi

I am slightly familiar with reflection but have never done the following
I know how to find a class and call but I haven't done the following

The Method return a List of Another Class
And I need to pass in an Enumeration which is in the class that contains the
method below

1)How do I create a List of this Class thru Reflection and once i populate it
how can I iterate thru it and get properties from the class

2)How can I pass in the Enumeration from this class ???

public List<TemplateInfo> GetTemplateList(FileType fileType)
{
}


Thanks
 
M

Marc Gravell

You you clarify some of that? It isn't entirely clear what you mean...
for example, what list do you want to create? You can create a
List<TemplateInfo> without reflection...? You can create lists via
reflection, but I'd like to understand the scenario first...

Re properties, you can use GetProperties() on a Type, and use
GetValue()/SetValue() to manipulate them... but again, if you could
clarify what type you are trying to manipulate it would help.

Marc
 
S

sippyuconn

Hi

I have an assembly I want to access thru Reflection

The assembly contains this Method below plus the Class that is
being returned in the list, also ths assembly contains the Enumeration being
passed in as input variable.

So thru reflection - how can I create a List of a Class in an Assemble
and also thru reflection how do I pass in an Enumeration defined in that
assembly ???

public List<TemplateInfo> GetTemplateList(FileType fileType)
{
}

Thanks for your time
 
M

Marc Gravell

You would have to use something like:

Assembly a = Assembly.LoadFile(path);
Type type = a.GetType(typeName),
enumType = a.GetType(enumTypeName);
object enumValue = Enum.Parse(enumType, enumName);
MethodInfo method = type.GetMethod("GetTemplateList");
object obj = Activator.CreateInstance(type); // since not
a static method
object list = method.Invoke(obj, new object[]
{ enumValue });

I can't verify it (for obvious reasons), but that should get you
started... you can use the non-generic IList interface to talk to the
list quite simply:

IList list2 = (IList) list;

However, using the generic form would be a pain...

Marc
 
J

Jialiang Ge [MSFT]

Hello Sippyuconn,

Apart from Marc's suggestions, you may also try this:

1. Create a list of TemplateInfo with .NET Reflection

string typeName =
"System.Collections.Generic.List`1[[TestNamespace.TemplateInfo,
TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]";
Type type = Type.GetType(typeName);
object o = Activator.CreateInstance(type);

,where typeName is the full type name of List<TemplateInfo>.
"TestNamespace" is the namespace of the class and TestAssembly is the
target assembly with strong name info.

You may get the type name in an easier way by not using Reflection first:
List<TemplateInfo> list = new List<TemplateInfo>();
Console.Write(list.GetType().FullName);

2. Iterate the list object with Reflection.

One solution is Marc's suggestion of converting the object to IList.
Another way is to invoke the "get_Item" method with Reflection:

a. Get the count of elements in the collection
type.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty,
null, o, null);

b. Get each object in a for statement:
object element = type.InvokeMember("get_Item",
System.Reflection.BindingFlags.InvokeMethod, null, l, new object[] { index
});

There might be other better ways to iterate the collection with .NET
Reflection. I appreciate other community members' inputs.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Marc Gravell

Re 1, that is the hard way - it is easier to get the Type for
TemplateInfo first, and then use typeof(List<>).MakeGenericType(type).

For the others, it would probably be easier to have a generic method:

Foo<T>(List<T> list) {...}

and use GetMethod("Foo").MakeGenericMethod(type).Invoke(...)

that way you are only doing reflcetion once; the code inside Foo<T> can do:

foreach(T t in list) {...}
T x = list[4];

etc

Marc
 

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