✅ Get type of current instance in a static method
how can i get the current type of the current class? this class will be inherited by other classes, and i need to get the hash code of the name of the class
42 Replies
That's some weird stuff you're doing there
Generally, the parent class has no knowledge of child classes
You can give the base class an
abstract
or virtual
property named Hash
or something, and each child class will have to implement it
But, yeah, curious why you even need to do that. I never see .GetHashCode()
used outside of implementing equality operators/interfaceshttps://github.com/RyanDeivert555/GameObject-Framework/blob/main/include/component.h
i want to translate this C++ code, but the code uses macros which arent in C#
GitHub
GameObject-Framework/include/component.h at main · RyanDeivert555/G...
A framework built on top of raylib that allows for the use of a game object system akin to Unity - RyanDeivert555/GameObject-Framework
its a GameObject system
i want to hash the names of the Components for easy storage in the game object
if you call
GetType()
that will get the actual type of the instance, but you probably don't want to be using reflectionYeah, reflection is slow
Wouldn't use it in a game
and the hashcode of a type name is certainly not a good thing to choose as an identifier
hashcodes aren't even guaranteed to be the same between application runs iirc
Yep
hmm i see
but there are no macros in c#, so what should i do?
if you need a type ID system, implement something that doesn't rely on how C# is implemented
even relying on type names will make your game brittle in the face of refactoring
if you want something that's like a macro, look into source generators
that's about the closest you'll get
alternatives would be abstract/virtual methods that you override in
Component
derivatives or an IComponent
interface to require implementing themI only have the id thing so i can store the components in a Dictionary for easy lookup
protected readonly Dictionary<int, Component> Components = [];
this is in the GameObject class
int is the id of the componentMake the key a GUID maybe
thats a good idea
if it's only at runtime why not just use the type as the key
wdym?
one problem with that is the method cant be static. ideally i would be able to do something like this;
So you can have only one component of each type?
wdym
If you want to be able to remove components by their type... what happens if there are multiple
FooComponent
s?
T
would be FooComponent
each game object can only have one type of each component. it cant have two
FooComponents
Ah, that would work then. Long as you ensure that it is the case
(ignore the id part right now) this would ensure that, right?
Components
is a dictionaryA dictionary cannot have conflicting keys
ok i think i can work from here. thank you guys
i want to make the ids static, but idk if thats possible
Sure is
Do note, though, that it will be different each time the app runs
derived classes will not have their own ids though
It will stay the same during each individual run, though
they will share the static id with the base class
You can make it a property and make it abstract
static abstract
properties are a thing nowadayspublic static readonly abstract Guid Id;
i get an error saying A static member cannot be marked as 'abstract'
C#/.NET version?
i downloaded .NET sdk 8.0
idk if that tells you what version i have
Check the csproj
its 6.0.0
Oof, two versions old
Yeah, no static abstract for you
i want the latest version
Change that
net6.0
to net8.0
thenwait no
its 8.0, i was looking at a dependency
mb
still have the error though
Ah, seems
static abstract
can only be declared in an interface: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/static-abstracts-in-interfacesdamn
Anyway, I gotta hit the hay now, so maybe Jim will be able to help some more
thank you for your help
Anytime
ok, if i want to use Guid's, i need static abstract i think because right now each instance has a new id, which isnt what a want
nevermind
protected readonly Dictionary<Guid, IComponent> Components = [];
cannot be done because of the static abstract