Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
like image 430
Faizan S. Avatar asked Dec 03 '22 15:12

Faizan S.


1 Answers

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);
like image 123
JaredPar Avatar answered Dec 20 '22 11:12

JaredPar