473,385 Members | 1,958 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,385 software developers and data experts.

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 2860
>How do i get the type info in a static method?
Only with typeof(YourClass). 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.googlegr oups.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 SetListViewColumnHeaders(Type type,
ListView.ColumnHeaderCollection headers) {
PropertyInfo[] infoList = type.GetProperties(BindingFlags.Public |
BindingFlags.Instance);

foreach (PropertyInfo p in infoList) {
foreach (Attribute attribute in Attribute.GetCustomAttributes(p)) {
GridViewColumnAttribute a = (GridViewColumnAttribute) attribute;

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

headers.Add(a.Name, a.Width).Tag = new GridViewColumnTag(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********@newsgroup.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
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 =...
4
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...
7
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}"...
4
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...
10
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...
8
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...
4
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....
9
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...
3
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 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.