reflection good course
Hello,
This is not a request for help with code. I already have used reflection to make several programs like a serialiser and deserialiser based on excel input / output. However it did take me a day or so to get it working because list objects cannot be cast to list. They need to be cast to IList. IList has some other issues.
Now I'm searching for a good course on reflection which also goes deep into the reflection and how to handle the objects.
The microsoft pages aren't very clear at least for me.
15 Replies
what do you mean "list objects cannot be cast to list?"
objects can be cast to any compatible type, so either the type itself, a type further up in the hierarchy or an interface they implement
Your question is too vague. I reckon there is no "good course" on reflection the way you expect. If you could tell us more about your specific goal, the problems you encountered and what approaches you tried, we could point you in a direction to solving that specific issue.
Reflection is the domain of last resort. You shouldn't be using reflection unless you have no other option.
Hi,
Sorry for the late reaction.
If you have an object defined in a list. e.g. List<Student>()
before you post lots of code, please use $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/$codegif
or $paste
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Keep in mind that this is reflection.
So themethod e.g.
public void Sort(object toSort) { // Here I can use Get properties as an array and get the list property. // After this I want to convert the property to a list or any Collection listproperty = (Ilist) theListProperty ==> this works listProperty = (List<>) theListProperty ==> this gives a runtime error. } Having this said. I already found away around this. So I'm searching for good documentation / course on reflection. But it seems not to be available. anyway thanks for the support
public void Sort(object toSort) { // Here I can use Get properties as an array and get the list property. // After this I want to convert the property to a list or any Collection listproperty = (Ilist) theListProperty ==> this works listProperty = (List<>) theListProperty ==> this gives a runtime error. } Having this said. I already found away around this. So I'm searching for good documentation / course on reflection. But it seems not to be available. anyway thanks for the support
You can't cast to
List<>
because theListProperty
is not implementing that type, i.e. you're missing the generic argument for T
. You can cast to IList
because that's a type actually imlemented by theListProperty
.
I highly doubt that you truly need reflection for your specific usecase.
In line with https://discord.com/channels/143867839282020352/1270000627736514580/1270139944148668506, please do consider redesigning your code to use strong, static typing instead of relying on reflection.Mayor McCheese
Reflection is the domain of last resort. You shouldn't be using reflection unless you have no other option.
Quoted by
<@293455315465273344> from #reflection good course (click here)
React with ❌ to remove this embed.
If you provide some more detail, we can help you refactor your code.
yes, because open generics can't be used like that
if you don't know the fully constructed type at compile time you'll be stuck in reflection the whole way unless you cast to a type that doesn't need that information
what actual problem are you trying to solve with reflection?