Search code examples
c#automatic-propertiesmethod-parameters

Auto-Implemented Property Classes as Parameter


I'd like to pass a property class reference into a method. For example:

Class SQLiteTables
{
    public class tblPersonnel
    {
        public int PsnID { get; set; }
        public string PsnFirstName { get; set; }
        public string PsnMiddleName { get; set; }
        public string PsnLastName { get; set; }
    }
    public class tblSchedules
    {
        public int SchID { get; set; }
        public string SchDescription { get; set; }
        public DateTime SchStartDtm { get; set; }
        public DateTime SchEndDtm { get; set; }

    ...

    public class TableName
    {
        public int Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }

        ...

        public string FieldN { get; set; }
    }
}

I would want to create a method something like this:

public void ThisMethod(PropertyClass propertyclassname)
{
        List<propertyclassname> TempList = dbConn.Table<propertyclassname>().ToList<propertyclassname>();
}

And use it like this:

ThisMethod(tblPersonnel);
ThisMethod(tblSchedules);

I'm trying to avoid making multiple methods for each property. I want it to be one reusable method but I can't seem to figure out how. Many thanks in advance!


Solution

  • You should use generics:

    public void ThisMethod<T>(T mySet) where T : MySetBaseClass
    {
        ...
    }
    

    What do you whant to do in the method and who is calling it?