Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建特定于类的泛型方法_C#_.net_Generics - Fatal编程技术网

C# 创建特定于类的泛型方法

C# 创建特定于类的泛型方法,c#,.net,generics,C#,.net,Generics,我已经创建了这个更新方法 public void Update(Person updated) { var oldProperties = GetType().GetProperties(); var newProperties = updated.GetType().GetProperties(); for (var i = 0; i < oldProperties.Length; i++) { var

我已经创建了这个更新方法

 public void Update(Person updated)
   {
       var oldProperties = GetType().GetProperties();
       var newProperties = updated.GetType().GetProperties();
       for (var i = 0; i < oldProperties.Length; i++)
       {
           var oldval = oldProperties[i].GetValue(this, null);
           var newval = newProperties[i].GetValue(updated, null);
           if (oldval != newval)
               oldProperties[i].SetValue(this, newval, null);
       }
   }
我正在考虑使用和抽象类来保存该方法,然后使用一些泛型,但我不知道如何使其特定于类。

尝试以下模式:

interface IUpdateable
{
void Update(IUpdateable updated)
}

public void Update<T>(T updated) where T:IUpdateable
{
...
...
}
接口可更新
{
无效更新(IUpdateable-updated)
}
公共无效更新(T更新),其中T:IUpdateable
{
...
...
}
尝试以下模式:

interface IUpdateable
{
void Update(IUpdateable updated)
}

public void Update<T>(T updated) where T:IUpdateable
{
...
...
}
接口可更新
{
无效更新(IUpdateable-updated)
}
公共无效更新(T更新),其中T:IUpdateable
{
...
...
}
公共静态无效更新(对象原始,对象更新)
{
var oldProperties=original.GetType().GetProperties();
var newProperties=updated.GetType().GetProperties();
对于(var i=0;i
或者,如果要确保类型相同:

   public static void Update<T>(T original, T updated)
   {
       var properties = typeof(T).GetProperties();
       for (var i = 0; i < properties.Length; i++)
       {
           var oldval = properties[i].GetValue(original, null);
           var newval = properties[i].GetValue(updated, null);
           if (!Equals(oldval,newval))
               properties[i].SetValue(original, newval, null);
       }
   }
公共静态无效更新(T原始,T更新)
{
var properties=typeof(T).GetProperties();
对于(var i=0;i
公共静态无效更新(对象原始,对象更新)
{
var oldProperties=original.GetType().GetProperties();
var newProperties=updated.GetType().GetProperties();
对于(var i=0;i
或者,如果要确保类型相同:

   public static void Update<T>(T original, T updated)
   {
       var properties = typeof(T).GetProperties();
       for (var i = 0; i < properties.Length; i++)
       {
           var oldval = properties[i].GetValue(original, null);
           var newval = properties[i].GetValue(updated, null);
           if (!Equals(oldval,newval))
               properties[i].SetValue(original, newval, null);
       }
   }
公共静态无效更新(T原始,T更新)
{
var properties=typeof(T).GetProperties();
对于(var i=0;i
您的代码有一个小缺陷,即如果您不强制执行这两个对象实际上是完全相同的类型,它们可能没有相同的属性,您将面临错误

像这样的通用方法应该可以在几乎任何东西上正确运行,只要它是
(这就是约束
中T:class
的作用:如果它不是要传递的类,代码就不会编译)

静态无效更新(T原始,T更新),其中T:class
{
var Properties=typeof(T).GetProperties();
foreach(属性中的PropertyInfo属性)
{
var oldval=property.GetValue(原始,空);
var newval=property.GetValue(已更新,空);
if(oldval!=newval)property.SetValue(原始、newval、null);
}
}

您的代码有一个小缺陷,即如果您不强制执行这两个对象实际上是完全相同的类型,它们可能没有相同的属性,您将面临错误

像这样的通用方法应该可以在几乎任何东西上正确运行,只要它是
(这就是约束
中T:class
的作用:如果它不是要传递的类,代码就不会编译)

静态无效更新(T原始,T更新),其中T:class
{
var Properties=typeof(T).GetProperties();
foreach(属性中的PropertyInfo属性)
{
var oldval=property.GetValue(原始,空);
var newval=property.GetValue(已更新,空);
if(oldval!=newval)property.SetValue(原始、newval、null);
}
}

此代码看起来不是特定于类的,可以将
Person
参数更改为
object
,它仍然可以工作。然后您可以将该方法放入名为
Updater
的类中,该类具有
Update(对象左,对象右)
并调用
newupdater().Update(c1,c2)。因此,简而言之,目前不一定需要泛型。请注意,您的oldval!=newval不健壮;不要使用该代码,因为该代码看起来不是特定于类的,
Person
参数可以更改为
object
,它仍然可以工作。然后您可以将该方法放入名为
Updater
的类中,该类具有
Update(对象左,对象右)
并调用
newupdater().Update(c1,c2)。因此,简而言之,目前不一定需要泛型。请注意,您的oldval!=newval不健壮;不要使用that@Pondidum我认为他的大部分答案都是简单地从OP中复制/粘贴的:-P@AdamHouldsworth我的也是:D@Pondidum我认为他的大部分答案都是简单地从OP中复制/粘贴的:-P@AdamHouldsworth我也是:如果每个想要使用
Update
的类都需要,那么这个模式是正确的公开
更新所需的内容。目前,除了
对象
的成员之外,
更新
不需要其他任何东西,所以这还不是必需的。@adamhuldsworth-你说得对。OP只需将参数类型更改为“object”,它可以用于任何类型。如果每个想要使用
Update
的类都需要公开
Update
所需的内容,则此模式是正确的。目前,除了
对象
的成员之外,
更新
不需要其他任何东西,所以这还不是必需的。@adamhuldsworth-你说得对。合作社
static void Update<T>(T original, T updated) where T : class
{
    var Properties = typeof(T).GetProperties();
    foreach (PropertyInfo property in Properties)
    {
        var oldval = property.GetValue(original, null);
        var newval = property.GetValue(updated, null);
        if (oldval != newval) property.SetValue(original, newval, null);
    }
}