473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Couple Reflection Questions

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<TemplateInfoGetTemplateList(FileType fileType)
{
}
Thanks
Oct 17 '08 #1
6 1481
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<TemplateInfowithout 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
Oct 18 '08 #2
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<TemplateInfoGetTemplateList(FileType fileType)
{
}

Thanks for your time
"Marc Gravell" wrote:
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<TemplateInfowithout 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
Oct 19 '08 #3
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
Oct 19 '08 #4
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<TemplateInfolist = 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 (ji****@online.microsoft.com, 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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 20 '08 #5
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<Tlist) {...}

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

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

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

etc

Marc
Oct 20 '08 #6
Thank you, Marc. -- Jialiang

Oct 20 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
7
by: maf | last post by:
Using reflection, I'm trying to get the value for a constant in an enum. Getting the contant name works fine using: FieldInfo fieldInfos = TYPE.GetFields(); foreach(FieldInfo fi in fieldInfos...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3...
5
by: rettigcd | last post by:
I have several classes that all have the same static member: class A{ public static string Table = "TableA"; } class B{ public static string Table = "TableB"; }
8
by: Rlrcstr | last post by:
If I have some code behind a form and I want to be able to pass an object to it... any of several user defined classes that I have... and then display the values in the member variables of the...
2
by: | last post by:
I'm writing web applications. I build and extend a lot of custom objects, and in the course of debugging my apps I invariably find myself writing a lot of junky code: Response.Write("Title: " +...
2
by: Doug | last post by:
Hi, I have two questions about Reflection. One I think I know the answer to but will ask anyway. 1) I can delve into a component or executable and get all the way to the method within a...
1
by: Patrick | last post by:
Has been trying to get an ASP.NET DLL's modification date/time for the "release date/time" to be displayed on the page's footer Using: System.Reflection.Assembly...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.