❔ Reflections and generics
So i have a type instance list and i'm iterating trough all the fields that the propertyType.Name.Contains("ExtensionConfig"), that type has a method which i need to invoke foreach field and i'm iterating trough the fields
So basically the problem comes when i do
(field as PropertyInfo).GetValue(type)
shows invalid cast exception, and i can't cast because the target type has a generic type which in every field is different52 Replies
in the foreach inside i'm iterating trough all the fields but i can't get it's value nor invoke any method inside
field is a
FieldInfo
, not a PropertyInfo
GetProperties() returns properties not fields
try GetFields instead
are u trying to return fields or properties? can u show ur class that ur trying to fetch fields or properties from?
so basically it's like this
this is the class i'm trying to get the fields
ok thats a property
those arent fields
yeah that is what i was getting
get properties
and so i need to access it's value, and it's value contains a method that is InitConfig
this is the type
and i need to run InitConfig basically but i always get that invalid cast exception
so can u show the image where its throwing invalid cast exception ?
sure
brb
this is bascially the error i get
i should mention this is BepInEx
that happens when i call
field.GetValue(type)
call it property not field, to avoid confustion
ok
so this Log.LogInfo line is throwing the error?
ye
whenever i invoke GetValue(type)
GetValue expects an instance, not a type
oh.
you are right
i don't need to iterate the types
but the instances
damn
no
i store the instances in a list
you need to iterate the types to get the properties
They probably want to iterate the instances, and call
GetType()
on each instance to get the typebut you need to know which instance to get the value of specified property
i have an idea
this should work
well if those properties are static, u dont need to pass an instance as an argument, just a
null
That assumes it's ok to create an instance and then discard it
And in that case you could potentially use static fields/properties instead of instance fields/properties
they aren't static
i know but im saying if they are
it adds it to a static list
i only need to run this one time
Oh right, I missed that bit
this should be able to invoke the InitValue method from the instance if i'm not wrong
try and see
sounds like method is null
imma see what methods there are
maybe i'm looking on the wrong place
GetMethod gets public methods by default
otherwise you need to pass BindingFlags argument
the method is public and non static
ok
yeah you typod
its InitConfig not InitValue
oops
Object does not match target type
show image, and brb
this is all it gives
a runtime error
oh
i see
it works now
thanks
well yes when the exception gets thrown, you can see in the debugger what type is being invoked
nop, i can't see because i can't debug like that
this is a class library
that gets ran by an injector
but it kinda works now
i just have a dilema which is kinda weird if imma say so myself
well use Console.WriteLine to debug
what are u trying to inject anyway?
it's a mod for crabgame
injected trough bepinex
as all the methods and stuff are obfuscated because the references are from il2cpp... i'm kinda making something to make this easier
what? does the game allow you to mod it?
well no
bepinex just searches the program memory or something
that can get you banned from this server tho
that are not hacks
it's nothing harmful at all
and also the game creator says he doesn't care meanwhile it's not harmful, well said once
he just didn't implement a way to mod the game cuz it doesn't get updated since 2 years ago
and people seeking ways to make it a better experience
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.