473,507 Members | 6,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get Type Of Class - Create Instance?

System.Drawing.SystemColors is a class with all static properties. Here is
what my short-ranged goal is...

I want to enumerate all of the SystemColors properties and print the name
(using reflection), and I am able to do this:

PropertyInfo[] props = typeof(SystemColors).GetProperties();
foreach (PropertyInfo prop in props) {
Console.WriteLine(prop.Name);
}
This works fine...now, in the same method, I want to get the value of each
property, which I can using the following:

SystemColors colors = null;
foreach (PropertyInfo prop in props) {
Console.WriteLine(prop.GetValue(null, null).ToString());
}

Now heres the problem, I want to pass the class to the method, so that the
class can be System.Drawing.Colors or System.Drawing.SystemColors, etc...

public static void DisplayColors(object Instance)
{
PropertyInfo[] props = Instance.GetType().GetProperties();
foreach (PropertyInfo prop in props) {
Console.WriteLine(
"Name: {0} - Value: {1}",
prop.Name, prop.GetValue(null, null).ToString()
);
}
}

What do I pass to the DisplayColors method? I can't pass
System.Drawing.SystemColors because it's a not-creatable class? How can I
get a reference to the static instance so I can pass it to DisplayColors?

Thanks,
Mythran

Mar 21 '06 #1
2 21416
Hi,

What if instead of passing an isntance you pass the type , you could do:

DisplayColors( typeof( SystemColors ));

DisplayColors( some_instance.GetType() );
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:eL**************@TK2MSFTNGP09.phx.gbl...
System.Drawing.SystemColors is a class with all static properties. Here
is what my short-ranged goal is...

I want to enumerate all of the SystemColors properties and print the name
(using reflection), and I am able to do this:

PropertyInfo[] props = typeof(SystemColors).GetProperties();
foreach (PropertyInfo prop in props) {
Console.WriteLine(prop.Name);
}
This works fine...now, in the same method, I want to get the value of each
property, which I can using the following:

SystemColors colors = null;
foreach (PropertyInfo prop in props) {
Console.WriteLine(prop.GetValue(null, null).ToString());
}

Now heres the problem, I want to pass the class to the method, so that the
class can be System.Drawing.Colors or System.Drawing.SystemColors, etc...

public static void DisplayColors(object Instance)
{
PropertyInfo[] props = Instance.GetType().GetProperties();
foreach (PropertyInfo prop in props) {
Console.WriteLine(
"Name: {0} - Value: {1}",
prop.Name, prop.GetValue(null, null).ToString()
);
}
}

What do I pass to the DisplayColors method? I can't pass
System.Drawing.SystemColors because it's a not-creatable class? How can I
get a reference to the static instance so I can pass it to DisplayColors?

Thanks,
Mythran

Mar 21 '06 #2

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e6**************@TK2MSFTNGP14.phx.gbl...
Hi,

What if instead of passing an isntance you pass the type , you could do:

DisplayColors( typeof( SystemColors ));

DisplayColors( some_instance.GetType() );
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


In VB.Net, I was doing the following:

Dim colors As SystemColors
DisplayColors(c)

lol I didn't realize I was still passing NULL! :p So, I changed the
parameter to System.Type and it works :)

Thanks,
Mythran

Mar 21 '06 #3

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

Similar topics

1
14887
by: MKoleoso | last post by:
Problem: C#- Unable to create instance of a class implementing from an interface I have: namespace someNamespace { public __gc class SomeClass1 { }
3
4715
by: Steph | last post by:
Hi, On my first oracle instance, I have a global type 'productarray' (CREATE OR REPLACE TYPE productarray AS TABLE OF VARCHAR(30)), I have a package procedure having myArray OUT productarray as...
39
2084
by: Quick Fox | last post by:
Hi All, Please help for following case: How to Load a Assembly from DLL file and create instance of the class in the loaded file. I want make a function that get 2 string parameters...
5
1979
by: mlev | last post by:
I want function in one namespace to create instance of class defined in another namespace. (more detailed - I try to make independent "Seralizer" dll, to be used by different application, each...
5
4107
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
4
2033
by: slaks | last post by:
During runtime i need to load some dll. Assembly.LoadFile works ok, but when i invoke CreateInstance on some type from this dll it throws me exception because my type depends on dll's which are in...
5
3155
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
0
1356
by: mkadasi | last post by:
I want to dynamically create instance of class stored in other application. I am writing the following code for this purpose: Here in the AssemblyFunc() I am loading the assembly of TariffClass...
1
1468
by: gayane | last post by:
Hi everone I have myobject of type object and i want to create instance of its real type. All types are standart types. From myobject.getType() and can get its type. But how to create instance ?...
0
7220
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
7105
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
7308
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
7371
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...
0
7479
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...
1
5037
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...
0
3188
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.