如何在C sharp中泛化代码

如何在C sharp中泛化代码

问题描述:

我是C#编程的新手。我有3种方法相同的内容但差异类型。我想为所有这些方法制作一个通用方法。但我不知道该怎么做。请帮我。非常感谢...



I'm new to C# programming. I have 3 methods same content but difference types. I want to make one generic method for all of them. But I don't know how to do it. Please help me. Thanks so much...

public class insDatalog
    {
        public ObjectId _id { get; set; }
        public string _date { get; set; }
        public string _lCode { get; set; }
        public string _lName { get; set; }
        public string _stage { get; set; }
        public string _pcName { get; set; }
        public string _serial { get; set; }
        public string _model { get; set; }
        public string _uCode { get; set; }
        public string _uName { get; set; }
        public string _result { get; set; }
    }


public class insUnitLib
    {
        public ObjectId _id { get; set; }
        public string _uCode { get; set; }
        public string _uName { get; set; }
        public string _date { get; set; }
    }


public class insSerialNo
    {
        public ObjectId _id { get; set; }
        public string _serial { get; set; }
    }

public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, insUnitLib t) where T: insUnitLib
        {
            collection.DeleteOne(b => b._id == t._id);
        }

public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, insDatalog t) where T : insDatalog
        {
            collection.DeleteOne(b => b._id == t._id);
        }

public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, insSerialNo t) where T : insSerialNo
        {
            collection.DeleteOne(b => b._id == t._id);
        }





我的尝试:



我试图在互联网上搜索但仍然没有结果。



What I have tried:

I have tried to search in the internet but still have no results.

您可以尝试创建一个界面并使您的三个类实现这个界面;然后,您可以将限制放在接口本身的泛型方法声明中。这样:

You can try to create an interface and make your three classes implement this interface; then you can place the restriction in your generic method's declaration on the interface itself. This way:
public interface IObjectWithId
{
   ObjectId _id { get; set; }
}

public class insDatalog : IObjectWithId
{
   // ...
}

public class insUnitLib : IObjectWithId
{
   // ...
}
public class insSerialNo : IObjectWithId
{
   // ...
}

public class YourContainerClass
{
   public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, T t) where T : IObjectWithId
   {
      collection.DeleteOne(b => b._id.Equals(t._id));
   }
}



它还假设你正确实现了 IEquatable< T> 您的 ObjectId 类/结构定义中的接口。



您也可以重新考虑您的命名方案,因为它不适合非常好地进入.NET世界,但这更像是一个化妆品的选择,你最终可以忽略它。



希望这会有所帮助。那么。


It also supposes that you correctly implemented the IEquatable<T> interface in your ObjectId class/struct definition.

You may also rethink your naming scheme as it does not fit very well into the .NET world, but that's more of a "cosmetic" choice remark which you can ignore, eventually.

Hope this helps. Kindly.


如果你的所有T类派生自相同的基类或接口,你可以使用它而不是中的单个类,其中T 子句(更好的解决方案!)



或者,您可以使用单个方法将所有类链接在一起(效率不高!):

If all of your T classes derived from the same base class or interface, you could use that instead of individual classes in the where T clause (better solution!)

OR, you could just use a single method and chain all the classes together (not really efficient!):
public static void Delete....<T>(IMongoCollection<T> collection, insUnitLib t) where T: insUnitLib, insDatalog, insSerialNo