0

I'm new to Reflection and Im just trying cool little ideas.

I'm trying to see if there is a way to find the type that a property is and pass it through a generic type parameter? If it's not possible that's ok, just would be fun to play around with, even if it's not that efficient

I have the field type, just need to have a way to convert it so it's acceptable by the compiler, unless generic type parameters need to be hard coded.

Here is an example of what I want to do:

foreach(FieldInfo f in typeof(ActivityData).GetFields())
{ 
    Database.GetData</*Do something here?*/>(nReader, "");
}

rtn.ActionID = Database.GetData<int>(nReader, GetAttribute<DBAttribute, ActivityData>(nameof(this.ActionID)).ColumnName);

So the last line is what I want to do, but for each field inside of my object, however, I'm not sure how to get the generic type parameter through reflection, or if its possible at all.

Skyhighjinks
  • 162
  • 1
  • 1
  • 10
  • there might be away to use `dynamic`... but IMO if you are only dealing with built in objects like `int`, `string`, etc, then the simplest way to do it would be a `switch` statement since the type inside the `< >` needs to be defined at compile time – chancea Oct 26 '21 at 17:37
  • This kind of code is almost always an anti-pattern and would benefit from a refactor. – DavidG Oct 26 '21 at 17:38
  • Lookup `MakeGenericType` and `MakeGenericMethod`. The former requires that you have the outer type (something like `MyGenericType<>` or `List<>`) and the type of the type parameter (`typeof(int)` in `List`). It's similar with `MakeGenericMethod`, but beyond what fits in a comment – Flydog57 Oct 26 '21 at 17:39

0 Answers0