簡體   English   中英

如何編寫代碼片段以生成C#中的方法?

[英]How to write a code snippet to generate a method in C#?

我想編寫一個執行以下操作的代碼段,例如如果我有一個類,比如說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. 如果是,我應該如何開始,有什么指導嗎?

您的摘要應該在

C:\\ Users \\ CooLMinE \\ Documents \\ Visual Studio(版本)\\代碼段\\ Visual C#\\我的代碼段

最簡單的方式是獲取現有的代碼片段並對其進行修改,以免重建文件的布局。

這是您可以使用的模板:

<?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/zh-cn/library/ms242312.aspx

如何使用實用程序方法:

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

請注意,如果要在緊密循環(例如)中使用它,可以對其進行極大地優化,但是這樣做需要具備元編程知識。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM