2

Is there some way to specify the type of the enclosing class declaration statically? If i had an instance, I could clearly use typeof(this), but statically I don't see a way.

Something like (where this_type is a placeholder):

public class Message
{
   public static readonly int SizeInBytes = Marshal.SizeOf(typeof(this_type));
}

Clearly, I could just use the actual type name, but I've got several classes that follow this pattern and would like something less copy/paste error prone.

Steve
  • 93
  • 4
  • 1
    [Here's a previous question asking the same thing](http://stackoverflow.com/q/13677237/382780). Refactoring tools have made me want it less over the years, but it would still be neat. – 31eee384 Aug 28 '15 at 15:54
  • If you had an instance, the expression `typeof(this)` would be invalid, since `typeof()` requires a type. The expression you would want is `this.GetType()`. – Fernando Matsumoto Aug 28 '15 at 16:00
  • Possible duplicate of [.NET: Determine the type of “this” class in its static method](https://stackoverflow.com/questions/2081612/net-determine-the-type-of-this-class-in-its-static-method) – jrh Aug 28 '18 at 12:26

5 Answers5

3

You can use MethodBase.GetCurrentMethod().DeclaringType but typeof(Message) is probably the cleaner way

public class  Message
{
   public static readonly int SizeInBytes = Marshal.SizeOf(MethodBase.GetCurrentMethod().DeclaringType);
}

Btw, you'll get a runtime exception when you execute this code as it is trying to get the size of a managed object.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • I do agree with that answer and that should be accepted as correct. The question was about getting the caller's size, not the method class owner's size... my fault :-) – Caverna Aug 28 '15 at 17:56
1

Perhaps:

public class Message
{
   public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Message));
}

This way, 'Message' can also be static.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Caverna
  • 461
  • 9
  • 16
1

typeof(Message) would be the closest you'd get here, but I think you'd need to use a struct rather than a class to do this from what I recall.

Chris Disley
  • 1,286
  • 17
  • 30
0

What about an extension method on the type and get it dynamically instead of pushing it to a readonly static variable?

public static class Extensions
{
   public static int SizeOfType(this System.Type tp) {
      return Marshal.SizeOf(tp);
   }

  public static int SizeOfObjectType(this object obj) {
      return obj.GetType().SizeOfType();
   }

}

// calling it from a method, 2 ways
var size1 = this.GetType().SizeOfType();
var size2 = this.SizeOfObjectType();
var size3 = typeof(string).SizeOfType();
var size4 = "what is my type size".SizeOfObjectType();
Igor
  • 60,821
  • 10
  • 100
  • 175
0

After a short google search, I've seen other people using reflection to accomplish what you are talking about, but that comes with the caveat that it is probably a lot more expensive than just typing out typeof(this_type). I'd sooner recommend just typing it out.

Type t = MethodBase.GetCurrentMethod().DeclaringType

.NET: Determine the type of “this” class in its static method

Community
  • 1
  • 1
Daniel Stone
  • 267
  • 3
  • 10