question

JesseKnott-2239 avatar image
0 Votes"
JesseKnott-2239 asked JesseKnott-2239 commented

Is it advisable to avoid using System.Reflection?

I heard before that using System.Reflection is ill advised.
I am trying to make a dynamic filter for data gotten form a sqlite database.

Right now, I have a base class that is more or less an overloaded shell for ObservableCollection<T>, that is inherited in a data class that I use to manage my collection of records.

I was thinking that I could use System.Reflection to make filtering the data even more dynamic.

I could basically do something like this...
Other than this being crazy confusing, as far as I know it should work....

  public Task<ObservableCollection<WeaponInventoryItem>> Filter(string key, string value)
         {
             FilteredList = (ObservableCollection<WeaponInventoryItem>)this.Items.Where(x => x.GetType().GetProperties().FirstOrDefault(y => y.Name.Equals(key)).Equals(value));
    
             return Task.FromResult(FilteredList);
         }

My alternative would be to make a switch based on the incoming key then just make case entries for each possible filter and make the appropriate `

      public Task<ObservableCollection<WeaponInventoryItem>> Filter(string key, string value)
       {
             switch(key)
             {
                  case "Manufacturer":
                       FilteredList = (ObservableCollection<WeaponInventoryItem>)this.Items.Where(x => x.Manufacturer.Equals(value)));
                       break;
             }
    
             return Task.FromResult(FilteredList);
       }

Any thoughts or suggestions?

Cheers!


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SimpleSamples avatar image
0 Votes"
SimpleSamples answered JesseKnott-2239 commented

You have two very different questions, as in the following:

  • Is it advisable to avoid using System.Reflection?

  • Any thoughts or suggestions? (about making a dynamic filter for data)


The guidance to avoid using Reflection does not say do not use it; it is to avoid it. If there is an alternative then that alternative should usually be used. One disadvantage of Reflection is performance, another is that it is a runtime thing so the compiler has less opportunity to catch errors.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That's more or less what I figured.
In the end, I just went with a less dynamic, but probably better design. I use a switch case and assign variables with the names of the fields from the table, then just execute a sql query selecting that data.

The overhead from using reflection is probably still a little more than handling the sql query and memory for the results.

Down the road I may try using the reflection route, but since the filtering is not truly open and dynamic, I can just code for the various scenarios that I support.

Cheers!

0 Votes 0 ·