Add support for Events (#23)
* event wip... * Add support for 'event' * event example
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
namespace DifferentNamespace
|
||||
{
|
||||
public class Address
|
||||
{
|
||||
public int HouseNumber { get; set; }
|
||||
}
|
||||
using System;
|
||||
|
||||
namespace DifferentNamespace
|
||||
{
|
||||
public class Address
|
||||
{
|
||||
public int HouseNumber { get; set; }
|
||||
|
||||
public event EventHandler<EventArgs> MyEvent;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace ProxyInterfaceConsumer
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person))]
|
||||
public partial interface IPerson
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using DifferentNamespace;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using DifferentNamespace;
|
||||
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
|
||||
@@ -1,146 +1,149 @@
|
||||
using AutoMapper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
private static JsonSerializerOptions JsonSerializerOptions = new ()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
IPersonT<int> pT = new PersonTProxy<int>(new PersonT<int>());
|
||||
pT.TVal = 1;
|
||||
Console.WriteLine(JsonSerializer.Serialize(pT, JsonSerializerOptions));
|
||||
Console.WriteLine(new string('-', 80));
|
||||
|
||||
IPersonTT<int, Program> pTT = new PersonTTProxy<int, Program>(new PersonTT<int, Program>());
|
||||
pTT.TVal1 = 42;
|
||||
pTT.TVal2 = new Program();
|
||||
Console.WriteLine(JsonSerializer.Serialize(pTT, JsonSerializerOptions));
|
||||
Console.WriteLine(new string('-', 80));
|
||||
|
||||
IPerson p = new PersonProxy(new Person());
|
||||
p.Name = "test";
|
||||
p.HelloWorld("stef");
|
||||
Console.WriteLine("DefaultValue " + p.DefaultValue());
|
||||
Console.WriteLine("DefaultValue " + p.DefaultValue(42));
|
||||
|
||||
//var ap = new AddressProxy(new Address { HouseNumber = 42 });
|
||||
//p.Address = ap;
|
||||
//var add = p.AddAddress(ap);
|
||||
//Console.WriteLine("add = " + JsonSerializer.Serialize(add, JsonSerializerOptions));
|
||||
//p.AddAddress(new AddressProxy(new Address { HouseNumber = 1000 }));
|
||||
|
||||
Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions));
|
||||
}
|
||||
}
|
||||
public struct Test
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public Clazz C { get; }
|
||||
|
||||
public IList<Clazz> Cs { get; set; }
|
||||
|
||||
public int Add(string s)
|
||||
{
|
||||
return 600;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Clazz
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public interface ITest
|
||||
{
|
||||
int Id { get; set; }
|
||||
|
||||
IClazz C { get; }
|
||||
|
||||
IList<IClazz> Cs { get; set; }
|
||||
|
||||
int Add(string s);
|
||||
}
|
||||
|
||||
public interface IClazz
|
||||
{
|
||||
string Name { get; set; }
|
||||
}
|
||||
|
||||
public class TestProxy : ITest
|
||||
{
|
||||
private Test _instance;
|
||||
|
||||
private IClazz _clazz;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public TestProxy(Test instance)
|
||||
{
|
||||
_instance = instance;
|
||||
|
||||
// _clazz = new ClazzProxy(_instance.C);
|
||||
|
||||
_mapper = new MapperConfiguration(cfg =>
|
||||
{
|
||||
//cfg.CreateMap<Clazz, ClazzProxy>();
|
||||
//cfg.CreateMap<ClazzProxy, Clazz>();
|
||||
|
||||
cfg.CreateMap<Clazz, IClazz>();
|
||||
cfg.CreateMap<IClazz, Clazz>();
|
||||
}).CreateMapper();
|
||||
}
|
||||
|
||||
public int Id
|
||||
{
|
||||
get => _instance.Id;
|
||||
set => _instance.Id = value;
|
||||
}
|
||||
|
||||
public IClazz C => _clazz;
|
||||
|
||||
public IList<IClazz> Cs2
|
||||
{
|
||||
get
|
||||
{
|
||||
//return null; // TinyMapper.Map<IList<IClazz>>(_instance.Cs); //(IList<IClazz>)_instance.Cs.Select(x => new ClazzProxy(x));
|
||||
return _mapper.Map<IList<IClazz>>(_instance.Cs); //(IList<IClazz>)_instance.Cs.Select(x => new ClazzProxy(x));
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_instance.Cs = _mapper.Map<IList<Clazz>>(value);
|
||||
//_instance.Cs = TinyMapper.Map<IList<Clazz>>(value);
|
||||
}
|
||||
}
|
||||
|
||||
public IList<IClazz> Cs
|
||||
{
|
||||
get => _mapper.Map<IList<IClazz>>(_instance.Cs);
|
||||
|
||||
set => _instance.Cs = _mapper.Map<IList<Clazz>>(value);
|
||||
}
|
||||
|
||||
public int Add(string s) => _instance.Add(s);
|
||||
}
|
||||
|
||||
public class ClazzProxy : IClazz
|
||||
{
|
||||
private Clazz _instance;
|
||||
|
||||
public ClazzProxy(Clazz instance)
|
||||
{
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
public string Name { get => _instance.Name; set => _instance.Name = value; }
|
||||
}
|
||||
}
|
||||
using AutoMapper;
|
||||
using DifferentNamespace;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
private static JsonSerializerOptions JsonSerializerOptions = new ()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
IPersonT<int> pT = new PersonTProxy<int>(new PersonT<int>());
|
||||
pT.TVal = 1;
|
||||
Console.WriteLine(JsonSerializer.Serialize(pT, JsonSerializerOptions));
|
||||
Console.WriteLine(new string('-', 80));
|
||||
|
||||
IPersonTT<int, Program> pTT = new PersonTTProxy<int, Program>(new PersonTT<int, Program>());
|
||||
pTT.TVal1 = 42;
|
||||
pTT.TVal2 = new Program();
|
||||
Console.WriteLine(JsonSerializer.Serialize(pTT, JsonSerializerOptions));
|
||||
Console.WriteLine(new string('-', 80));
|
||||
|
||||
var ap = new AddressProxy(new Address { HouseNumber = 42 });
|
||||
ap.HouseNumber = -1;
|
||||
ap.MyEvent += delegate (object x, EventArgs a)
|
||||
{
|
||||
};
|
||||
|
||||
IPerson p = new PersonProxy(new Person());
|
||||
p.Name = "test";
|
||||
p.HelloWorld("stef");
|
||||
// p.Address = ap;
|
||||
|
||||
Console.WriteLine("DefaultValue " + p.DefaultValue());
|
||||
Console.WriteLine("DefaultValue " + p.DefaultValue(42));
|
||||
|
||||
Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions));
|
||||
}
|
||||
}
|
||||
public struct Test
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public Clazz C { get; }
|
||||
|
||||
public IList<Clazz> Cs { get; set; }
|
||||
|
||||
public int Add(string s)
|
||||
{
|
||||
return 600;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Clazz
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public interface ITest
|
||||
{
|
||||
int Id { get; set; }
|
||||
|
||||
IClazz C { get; }
|
||||
|
||||
IList<IClazz> Cs { get; set; }
|
||||
|
||||
int Add(string s);
|
||||
}
|
||||
|
||||
public interface IClazz
|
||||
{
|
||||
string Name { get; set; }
|
||||
}
|
||||
|
||||
public class TestProxy : ITest
|
||||
{
|
||||
private Test _instance;
|
||||
|
||||
private IClazz _clazz;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public TestProxy(Test instance)
|
||||
{
|
||||
_instance = instance;
|
||||
|
||||
// _clazz = new ClazzProxy(_instance.C);
|
||||
|
||||
_mapper = new MapperConfiguration(cfg =>
|
||||
{
|
||||
//cfg.CreateMap<Clazz, ClazzProxy>();
|
||||
//cfg.CreateMap<ClazzProxy, Clazz>();
|
||||
|
||||
cfg.CreateMap<Clazz, IClazz>();
|
||||
cfg.CreateMap<IClazz, Clazz>();
|
||||
}).CreateMapper();
|
||||
}
|
||||
|
||||
public int Id
|
||||
{
|
||||
get => _instance.Id;
|
||||
set => _instance.Id = value;
|
||||
}
|
||||
|
||||
public IClazz C => _clazz;
|
||||
|
||||
public IList<IClazz> Cs2
|
||||
{
|
||||
get
|
||||
{
|
||||
//return null; // TinyMapper.Map<IList<IClazz>>(_instance.Cs); //(IList<IClazz>)_instance.Cs.Select(x => new ClazzProxy(x));
|
||||
return _mapper.Map<IList<IClazz>>(_instance.Cs); //(IList<IClazz>)_instance.Cs.Select(x => new ClazzProxy(x));
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_instance.Cs = _mapper.Map<IList<Clazz>>(value);
|
||||
//_instance.Cs = TinyMapper.Map<IList<Clazz>>(value);
|
||||
}
|
||||
}
|
||||
|
||||
public IList<IClazz> Cs
|
||||
{
|
||||
get => _mapper.Map<IList<IClazz>>(_instance.Cs);
|
||||
|
||||
set => _instance.Cs = _mapper.Map<IList<Clazz>>(value);
|
||||
}
|
||||
|
||||
public int Add(string s) => _instance.Add(s);
|
||||
}
|
||||
|
||||
public class ClazzProxy : IClazz
|
||||
{
|
||||
private Clazz _instance;
|
||||
|
||||
public ClazzProxy(Clazz instance)
|
||||
{
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
public string Name { get => _instance.Name; set => _instance.Name = value; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
using ProxyInterfaceSourceGenerator.Extensions;
|
||||
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
|
||||
using ProxyInterfaceSourceGenerator.Utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
{
|
||||
@@ -47,6 +48,8 @@ namespace {ns}
|
||||
{GenerateProperties(targetClassSymbol, proxyAll)}
|
||||
|
||||
{GenerateMethods(targetClassSymbol)}
|
||||
|
||||
{GenerateEvents(targetClassSymbol)}
|
||||
}}
|
||||
}}";
|
||||
|
||||
@@ -89,5 +92,19 @@ namespace {ns}
|
||||
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
private string GenerateEvents(INamedTypeSymbol targetClassSymbol)
|
||||
{
|
||||
var str = new StringBuilder();
|
||||
foreach (var @event in MemberHelper.GetPublicEvents(targetClassSymbol))
|
||||
{
|
||||
var ps = @event.First().Parameters.First();
|
||||
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
|
||||
str.AppendLine($" event {type} {@event.Key.GetSanitizedName()};");
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
return str.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -20,7 +20,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
{
|
||||
foreach (var ci in _context.CandidateInterfaces)
|
||||
{
|
||||
yield return GenerateFile(ci.Value); //.Namespace, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
|
||||
yield return GenerateFile(ci.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,8 @@ namespace {ns}
|
||||
|
||||
{GeneratePublicMethods(targetClassSymbol)}
|
||||
|
||||
{GenerateEvents(targetClassSymbol)}
|
||||
|
||||
public {constructorName}({targetClassSymbol} instance)
|
||||
{{
|
||||
_Instance = instance;
|
||||
@@ -197,5 +199,31 @@ namespace {ns}
|
||||
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
private string GenerateEvents(INamedTypeSymbol targetClassSymbol)
|
||||
{
|
||||
var str = new StringBuilder();
|
||||
foreach (var @event in MemberHelper.GetPublicEvents(targetClassSymbol))
|
||||
{
|
||||
var name = @event.Key.GetSanitizedName();
|
||||
var ps = @event.First().Parameters.First();
|
||||
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
|
||||
str.Append($" public event {type} {name} {{");
|
||||
|
||||
if (@event.Any(e => e.MethodKind == MethodKind.EventAdd))
|
||||
{
|
||||
str.Append($" add {{ _Instance.{name} += value; }}");
|
||||
}
|
||||
if (@event.Any(e => e.MethodKind == MethodKind.EventRemove))
|
||||
{
|
||||
str.Append($" remove {{ _Instance.{name} -= value; }}");
|
||||
}
|
||||
|
||||
str.AppendLine(" }");
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
return str.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using ProxyInterfaceSourceGenerator.FileGenerators;
|
||||
|
||||
@@ -31,6 +31,23 @@ namespace ProxyInterfaceSourceGenerator.Utils
|
||||
filter);
|
||||
}
|
||||
|
||||
public static IEnumerable<IGrouping<ISymbol, IMethodSymbol>> GetPublicEvents(INamedTypeSymbol classSymbol, Func<IMethodSymbol, bool>? filter = null)
|
||||
{
|
||||
if (filter is null)
|
||||
{
|
||||
filter = _ => true;
|
||||
}
|
||||
|
||||
#pragma warning disable CS8619 // Nullability of reference types in value doesn't match target type.
|
||||
#pragma warning disable RS1024 // Compare symbols correctly
|
||||
return GetPublicMembers(classSymbol,
|
||||
m => m.MethodKind == MethodKind.EventAdd || m.MethodKind == MethodKind.EventRemove/* || m.MethodKind == MethodKind.EventRaise*/,
|
||||
filter)
|
||||
.GroupBy(e => e.AssociatedSymbol);
|
||||
#pragma warning restore RS1024 // Compare symbols correctly
|
||||
#pragma warning restore CS8619 // Nullability of reference types in value doesn't match target type.
|
||||
}
|
||||
|
||||
// TODO : do we need also to check for "SanitizedName()" here?
|
||||
private static IEnumerable<T> GetPublicMembers<T>(INamedTypeSymbol classSymbol, params Func<T, bool>[] filters) where T : ISymbol
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user