How to write a code snippet to create a method in C #?

I want to write a piece of code that does the following, for example, if I have a class, say MyClass:

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

therefore, the fragment should create the following method:

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

So, the idea is that if I call a fragment for any class, it should create a DoUpdate method and should write all the properties as I did in the above example.

So I want to know:

  • Is it possible to do higher?
  • If so, how do I get started, any recommendations?
+6
source share
2 answers

How about the utility:

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

using an example:

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

Please note that this can be optimized very much if it is used in a narrow loop (etc.), but this requires knowledge of metaprograms.

+1
source

Your fragments should be under

C: \ Users \ CooLMinE \ Documents \ Visual Studio (version) \ Code Snippets \ Visual C # \ My Code Snippets

The easiest way to take an existing fragment and change it to avoid restoring the layout of the file.

Here is a template you can work with:

 <?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> 

This can come in handy when you want it to create names based on the class name and so on: http://msdn.microsoft.com/en-us/library/ms242312.aspx

+1
source

Source: https://habr.com/ru/post/924356/


All Articles