473,732 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<TemplateIn foGetTemplateLi st(FileType fileType)
{
}
Thanks
Oct 17 '08 #1
6 1517
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<TemplateIn fowithout 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<TemplateIn foGetTemplateLi st(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<TemplateIn fowithout 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.LoadFi le(path);
Type type = a.GetType(typeN ame),
enumType = a.GetType(enumT ypeName);
object enumValue = Enum.Parse(enum Type, enumName);
MethodInfo method = type.GetMethod( "GetTemplateLis t");
object obj = Activator.Creat eInstance(type) ; // since not
a static method
object list = method.Invoke(o bj, 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.Collect ions.Generic.Li st`1[[TestNamespace.T emplateInfo,
TestAssembly, Version=1.0.0.0 , Culture=neutral , PublicKeyToken= null]]";
Type type = Type.GetType(ty peName);
object o = Activator.Creat eInstance(type) ;

,where typeName is the full type name of List<TemplateIn fo>.
"TestNamesp ace" 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<TemplateIn folist = new List<TemplateIn fo>();
Console.Write(l ist.GetType().F ullName);

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.InvokeMemb er("Count", System.Reflecti on.BindingFlags .GetProperty,
null, o, null);

b. Get each object in a for statement:
object element = type.InvokeMemb er("get_Item",
System.Reflecti on.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****@microsof t.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<Tli st) {...}

and use GetMethod("Foo" ).MakeGenericMe thod(type).Invo ke(...)

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
1377
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 what Reflection is (I can answer this question myself), what kind of Reflection Techniques there are (.NET Reflection, Python Reflection etc. I can answer this one myself too). The main question I need to answer is related to the implementation of...
7
6372
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 ) { Console.WriteLine("Name: {0}" fi.Name); Console.WriteLine("Value: {0} " fi.GetValue(null).ToString());
8
16916
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
2
2986
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 types of items in the very hierarchical data model: - Classes (and nested classes) - Collections - Properties I've successfully written the Reflection code to handle any combination of classes and properties but I'm confused about what to do...
5
6804
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
292
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 class, I'm guessing that I use Reflection. I have never used anything like that before. Can someone point me to a good example of doing this? I will basically be drawing a table on the form with dta from the passed class in columns. The...
2
1881
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: " + uwtm.Title + "<br>"); Response.Write("Pubdate: " + uwtm.Pubdate + "<br>"); Response.Write("Byline String: " + uwtm.Byline + "<br>"); Response.Write("Order (converted): " + uwtm.Order + "<br>"); Response.Write("Body:" + uwtm.Body + "<br>");...
2
1535
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 class. But I can't see the code inside the method. Is there a way to get access to that? 2) This may be a dumb question but I'll ask. Is there anyway to use Reflection with a VB6 dll or exe?
1
17828
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 asm=System.Reflection.Assembly.GetExecutingAssembly(); appVer = asm.GetName().Version.ToString(); // get the location of our executing assembly System.IO.FileInfo inf = new System.IO.FileInfo(asm.Location);
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.