473,546 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type info in static method

mfc
How do i get the type info in a static method?

for instance in the code below is it possible for the Method to get the type
to know what type was used to call Method?

thanks

class C {
public C() {
}

public static Method() {
}
}

Class D : C {
}

C c = new C();
D d = new D();
d.Method();
c.cMethod;
Nov 27 '06 #1
4 2873
>How do i get the type info in a static method?
Only with typeof(YourClas s). Since it's a static method there's no
'this' reference to get type info from.

>for instance in the code below is it possible for the Method to get the type
to know what type was used to call Method?
That code shouldn't even compile since you can't call static methods
on instances. But if you write

D.Method();

it will still compile as

C.Method();

so there's no way for the code in Method to tell the difference.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 27 '06 #2
You know the type info in a static method, so there's no reason to get
it from anywhere. Static methods are contained and runnable only in a
given type, so the current type, and therefore the type info, is known
at design time and fixed.

In your example, you seem to want to know if you're calling Method() on
C or D. The fact is that you're always and only calling it on C,
because there is no D.Method() per se. D appears to have a static
Method() because it inherited it from its base, C, but you're still
calling it on C, not D.

If you want D.Method() to do something different from C.Method(),
create the following:

class D : C
{
public new static void Method() { /* do something different */ }
}

As noted elsewhere, this is a good example of why you should try to
compile any code that you post before you post it. If you'd done that,
you would have noticed several errors in your code that probably would
have tipped you off to the answer.
Stephan

mfc wrote:
How do i get the type info in a static method?

for instance in the code below is it possible for the Method to get the type
to know what type was used to call Method?

thanks

class C {
public C() {
}

public static Method() {
}
}

Class D : C {
}

C c = new C();
D d = new D();
d.Method();
c.cMethod;
Nov 27 '06 #3
mfc

"ssamuel" <ss*****@gmail. comwrote in message
news:11******** **************@ f16g2000cwb.goo glegroups.com.. .
You know the type info in a static method, so there's no reason to get
it from anywhere. Static methods are contained and runnable only in a
given type, so the current type, and therefore the type info, is known
at design time and fixed.
Well, I guess thats a limitation. I can think of many reasons why I would
want to know the type in a static method and won't know the type at compile
time. For instance suppose I wanted a class that contains data for a
listview that could query itself using reflection and find all its public
properties and then use that information to generate the column headers of a
listview. At the initialization stage when its setting up the UI, there
won't be an instance of the class since it hasn't hit the database yet, but
the class inherently contains all the information needed to set up the
coloumn headers and can do so via a static method. But the problem is that
any classes based on this class doesn't know what its type is unless its
passed in
public abstract class ListViewData{
public ListViewData() {
}

public abstract object this[string index] {
get;
set;
}

/// <summary>
/// Sets up the column headers for the ListView
/// </summary>
public static void SetListViewColu mnHeaders(Type type,
ListView.Column HeaderCollectio n headers) {
PropertyInfo[] infoList = type.GetPropert ies(BindingFlag s.Public |
BindingFlags.In stance);

foreach (PropertyInfo p in infoList) {
foreach (Attribute attribute in Attribute.GetCu stomAttributes( p)) {
GridViewColumnA ttribute a = (GridViewColumn Attribute) attribute;

if (a == null || a.Include == false)
continue;

headers.Add(a.N ame, a.Width).Tag = new GridViewColumnT ag(p.Name,
p.PropertyType, a.Flags);
}
}
}
}
In your example, you seem to want to know if you're calling Method() on
C or D. The fact is that you're always and only calling it on C,
because there is no D.Method() per se. D appears to have a static
Method() because it inherited it from its base, C, but you're still
calling it on C, not D.

Nov 28 '06 #4
"mfc" <mf********@new sgroup.nospama écrit dans le message de news:
ur************* *@TK2MSFTNGP04. phx.gbl...
| For instance suppose I wanted a class that contains data for a
| listview that could query itself using reflection and find all its public
| properties and then use that information to generate the column headers of
a
| listview. At the initialization stage when its setting up the UI, there
| won't be an instance of the class since it hasn't hit the database yet,
but
| the class inherently contains all the information needed to set up the
| coloumn headers and can do so via a static method. But the problem is that
| any classes based on this class doesn't know what its type is unless its
| passed in

But surely that is what you already have with the following :

{
PropertyInfo[] properties = typeof(MyType). GetProperties(. ..);

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 28 '06 #5

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

Similar topics

14
7394
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 = System.Int32.Parse(TextBox2.Text); //method 2 And people said "int" is a C# alias for "System.Int32". If this is the case, can we say "int" is an object??
4
1338
by: Jamie B | last post by:
Is it possible to use System.Type, Activator or whatever to create a complete "blank", unitialized object of a given type, and then at a later time call a constructor on it. The only caveat is that before I call the constructor I still want the object to correctly return its type if GetType() is called. If its possible, how do I create the...
7
7800
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put...
4
3134
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my question In short my question is why when I pass a generic type directly to the formatObject function it works fine, but when I pass it to the checkText...
10
2224
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long before the static ctor fires. With a generic class and the "new()" clause, I can force this in the static ctor of the generic class, but this is...
8
1236
by: Bob Rock | last post by:
Hello, is there a way of retrieving the type of the class containing a static method from the method itself when it gets called? I have this situation: an abstact class that defines a static method, a class that derives from the abstract class and I need to retrieve the type of the concrete class when calling the static method. public...
4
3121
nomad
by: nomad | last post by:
Hello Everyone... I have rewrite my code and I ran into some problems. I have two problems. 1. What does this mean "Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic type ArrayList<E> should be parameterized" at this statement arlist.add(temp); 2. how do I change this "Cannot make a static...
9
5833
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context,...
3
14989
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 Cannot implicitly convert type 'System.Collections.Generic.List<Prsym.ComboPopulation.ComboInfo>' to 'System.Collections.Generic.List<T> '
0
7504
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...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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...
0
6026
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...
0
3491
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.