1

我想将属性类引用传递给方法。例如:

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; }
    }
}

我想创建一个类似这样的方法:

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

并像这样使用它:

ThisMethod(tblPersonnel);
ThisMethod(tblSchedules);

我试图避免为每个属性创建多种方法。我希望它是一种可重复使用的方法,但我似乎无法弄清楚如何。提前谢谢了!

4

3 回答 3

1

您应该使用泛型:

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

你想在方法中做什么,谁在调用它?

于 2014-02-14T10:01:13.883 回答
0

您要完成的实际逻辑并不容易,但是如果将其更改为以下内容,您可能会解决它

public abstract class PropertySetBase
     {
           public  abstract int Property_int1 { get; set; }
        public abstract string Property1 { get; set; }
        public abstract string Property2 { get; set; }
        public  abstract  string Property3 { get; set; }
     }   

头等舱

    public class PropertySet1:PropertySetBase
    {

    public override int  Property_int1
{
      get 
    { 

    }
      set 
    { 
    }
}

public override string  Property1
{
      get 
    { 
    }
      set 
    { 

    }
}

public override string  Property2
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property3
{
      get 
    {   
    }
      set 
    { 
    }
}
}

二等

    public class PropertySet2:PropertySetBase
    {

public override int  Property_int1
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property1
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property2
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property3
{
      get 
    { 

    }
      set 
    { 

    }
}
}

以及您的方法应该如何编写以接受任何属性类

public void ThisMethod( PropertySetBase propertyclassname)
{

}
于 2014-02-14T10:00:30.367 回答
0

我认为您想要重复使用属性集类型,为此您可以使用 IEnumerable

public class PropertySet
{
        public int Property0 { get; set; }
        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; }
}

在您的方法中使用这种类型的列表,以便在您的方法中使用尽可能多的项目

public void ThisMethod(List<PropertySet> propertyclass)
{

}
于 2014-02-14T10:17:52.723 回答