2012-09-01 39 views
6

我想写这不下面的东西,就像如果我有一个类假设MyClass的代码片段:如何编写代码片段以在C#中生成方法?

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. 如果是,我应该如何开始,任何指导?
+0

为什么不使用一些OOP概念? – Tigran

+0

有什么要求?你只是在寻找房产?你想要静态属性吗? – itsme86

+0

只有属性... –

回答

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

请注意,如果要在严格的循环(等)中使用,这可能会被极大地优化,但这样做需要元编程知识。

+0

如果我的MyClass中有其他类型属性,该怎么办? –

+0

这是非常好的解决方案,但实际上它是也可以编写一个片段做同样的? –

+0

@Praveen定义“其他类型”;重新片段 - 我不知道 –

1

你的片段应该是下

C:\用户\ CooLMinE \文档\ 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/en-us/library/ms242312.aspx

+0

我在这里建议了类似的东西http://stackoverflow.com/a/19247785/687441,但只能创建一个空方法。通常我会用财产片段(丙或propfull 其中是键盘上的Tab键)创建的属性,但如果它总是相同的代码coolmine建议你可以硬编码的性能;尽管如此,还不够好用。 –

相关问题