6

クラスがある場合、MyClassとしましょう:

   class MyClass
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }

したがって、スニペットは次のメソッドを作成する必要があります。

 public bool DoUpdate(MyClass myClass)
  {
        bool isUpdated = false;
        if (Age != myClass.Age)
        {
            isUpdated = true;
            Age = myClass.Age;
        }
        if (Name != myClass.Name)
        {
            isUpdated = true;
            Name = myClass.Name;
        }
        return isUpdated;
    }

したがって、任意のクラスのスニペットを呼び出すと、DoUpdateメソッドを作成し、上記の例で行った方法ですべてのプロパティを書き込む必要があります。

だから私は知りたい:

  1. 上記を行うことは可能ですか?
  2. はいの場合、どのように開始すればよいですか?
4

2 に答える 2

1

代わりにユーティリティメソッドはどうですか:

public static class MyUtilities
{
    public static bool DoUpdate<T>(
        this T target, T source) where T: class
    {
        if(target == null) throw new ArgumentNullException("target");
        if(source == null) throw new ArgumentNullException("source");

        if(ReferenceEquals(target, source)) return false;
        var props = typeof(T).GetProperties(
            BindingFlags.Public | BindingFlags.Instance);
        bool result = false;
        foreach (var prop in props)
        {
            if (!prop.CanRead || !prop.CanWrite) continue;
            if (prop.GetIndexParameters().Length != 0) continue;

            object oldValue = prop.GetValue(target, null),
                   newValue = prop.GetValue(source, null);
            if (!object.Equals(oldValue, newValue))
            {
                prop.SetValue(target, newValue, null);
                result = true;
            }
        }
        return result;
    }
}

使用例:

var a = new MyClass { Name = "abc", Age = 21 };
var b = new MyClass { Name = "abc", Age = 21 };
var c = new MyClass { Name = "def", Age = 21 };

Console.WriteLine(a.DoUpdate(b)); // false - the same
Console.WriteLine(a.DoUpdate(c)); // true - different

Console.WriteLine(a.Name); // "def" - updated
Console.WriteLine(a.Age);

タイトなループ (など) で使用する場合、これを大幅に最適化できますが、そのためにはメタプログラミングの知識が必要であることに注意してください。

于 2012-09-01T19:06:28.643 に答える
1

あなたのスニペットは下にあるべきです

C:\Users\CooLMinE\Documents\Visual Studio (バージョン)\Code Snippets\Visual C#\My Code Snippets

ファイルのレイアウトの再構築を避けるために、既存のスニペットを取得して変更する最も簡単な方法です。

操作できるテンプレートは次のとおりです。

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>snippetTitle</Title>
            <Shortcut>snippetShortcutWhichYouWillUseInVS</Shortcut>
            <Description>descriptionOfTheSnippet</Description>
            <Author>yourname</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                </Literal>
                <Literal Editable="false">
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[yourcodegoeshere]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

これは、クラス名などに基づいて名前を生成する場合に便利です: http://msdn.microsoft.com/en-us/library/ms242312.aspx

于 2012-09-01T19:00:13.623 に答える