Search code examples
c#.netfunctiondb4oargument-passing

Querying by type in DB4O


How do you pass a class type into a function in C#?

As I am getting into db4o and C# I wrote the following function after reading the tutorials:

    public static void PrintAllPilots("CLASS HERE", string pathToDb)
    {
        IObjectContainer db = Db4oFactory.OpenFile(pathToDb);
        IObjectSet result = db.QueryByExample(typeof("CLASS HERE"));
        db.Close();
        ListResult(result);
    }

Solution

  • There are two ways. The first is to explicitly use the Type type.

    public static void PrintAllPilots(Type type, string pathToDb)
    {
      ...
      IObjectSet result = db.QueryByExample(type);
    }
    
    PrintAllPilots(typeof(SomeType),somePath);
    

    The second is to use generics

    public static void PrintAllPilots<T>(string pathToDb)
    {
      ...
      IObjectSet result = db.QueryByExample(typeof(T));
    }
    
    PrintAllPilots<SomeType>(somePath);