RSCG Example–Static To Interface–part 24

 

 

name Static To Interface
nuget

https://www.nuget.org/packages/RSCG_Static/

link http://msprogrammer.serviciipeweb.ro/category/roslyn/
author Andrei Ignat

This will generate code for any static properties of a class to generate interface, record and a class with real behaviour
 

The code that you start with is

1
2
3
4
5
6
7
public partial class Helpers
 
{
 
public partial ISystem_DateTime FromStaticDate();
 
}

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
12
13
var dateStatic1 = (new Helpers().FromStaticDate());//static       
 
var dateStatic2 = recISystem_DateTime.MakeNew();//static
 
var dateVar3 = new clsISystem_DateTime(); //variable = real
 
await Task.Delay(10 * 1000);
 
Console.WriteLine(dateStatic1.Now.Second);
 
Console.WriteLine(dateStatic2.Now.Second);
 
Console.WriteLine(dateVar3.Now.Second);

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace RSCG_Static_Console {
 
public interface ISystem_DateTime {
 
System.DateTime Now  {get;}
 
System.DateTime UtcNow  {get;}
 
System.DateTime Today  {get;}
 
}// interface
 
//now the partial class
 
public record recISystem_DateTime (System.DateTime Now,System.DateTime UtcNow,System.DateTime Today) : ISystem_DateTime
 
{
 
public static recISystem_DateTime MakeNew() {
 
return new recISystem_DateTime(System.DateTime.Now,System.DateTime.UtcNow,System.DateTime.Today);
 
} //end makenew
 
} //end record
 
public class clsISystem_DateTime : ISystem_DateTime
 
{
 
public  System.DateTime Now  {get { return System.DateTime.Now; } }
 
public  System.DateTime UtcNow  {get { return System.DateTime.UtcNow; } }
 
public  System.DateTime Today  {get { return System.DateTime.Today; } }
 
} //end record
 
partial class Program {
 
public partial ISystem_DateTime FromStaticDate() {
 
return recISystem_DateTime.MakeNew();
 
} // method
 
} // class
 
} // namespace

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/StaticToInterface