mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-04-29 06:48:41 +00:00
Tests
This commit is contained in:
@@ -117,30 +117,6 @@ namespace Esiur.Proxy
|
||||
}
|
||||
}
|
||||
|
||||
// var code = @$"using Esiur.Resource;
|
||||
//using Esiur.Core;
|
||||
|
||||
//#nullable enable
|
||||
|
||||
//namespace {ci.ClassSymbol.ContainingNamespace.ToDisplayString()} {{
|
||||
//";
|
||||
|
||||
// if (IsInterfaceImplemented(ci, classes))
|
||||
// code += $"public partial class {ci.Name} {{\r\n";
|
||||
// else
|
||||
// {
|
||||
// code +=
|
||||
//$@" public partial class {ci.Name} : IResource {{
|
||||
// public virtual Instance? Instance {{ get; set; }}
|
||||
// public virtual event DestroyedEvent? OnDestroy;
|
||||
|
||||
// public virtual void Destroy() {{ OnDestroy?.Invoke(this); }}
|
||||
//";
|
||||
|
||||
// if (!ci.HasTrigger)
|
||||
// code += "\tpublic virtual AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n\r\n";
|
||||
// }
|
||||
|
||||
foreach (var f in ci.Fields)
|
||||
{
|
||||
var givenName = f.GetAttributes().FirstOrDefault(x => x.AttributeClass?.Name == "ExportAttribute")?.ConstructorArguments.FirstOrDefault().Value as string;
|
||||
@@ -156,12 +132,17 @@ namespace Esiur.Proxy
|
||||
|
||||
if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler"))
|
||||
{
|
||||
code.AppendLine($"public event {f.Type} {pn};");
|
||||
code.AppendLine($"\tpublic event {f.Type} {pn};");
|
||||
}
|
||||
else
|
||||
{
|
||||
code.AppendLine($"\t{attrs}\r\n\t public {f.Type} {pn} {{ \r\n\t\t get => {fn}; \r\n\t\t set {{ \r\n\t\t this.{fn} = value; \r\n\t\t Instance?.Modified(); \r\n\t\t}}\r\n\t}}\r\n");
|
||||
|
||||
code.AppendLine($"\tpublic {f.Type} {pn} {{");
|
||||
code.AppendLine($"\t\t get => {fn};");
|
||||
code.AppendLine($"\t\t set {{");
|
||||
code.AppendLine($"\t\t this.{fn} = value;");
|
||||
code.AppendLine($"\t\t Instance?.Modified();");
|
||||
code.AppendLine($"\t\t}}");
|
||||
code.AppendLine($"\t}}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Esiur.Proxy;
|
||||
public static class ResourceProxy
|
||||
{
|
||||
static Dictionary<Type, Type> cache = new Dictionary<Type, Type>();
|
||||
static object cacheLock = new object();
|
||||
|
||||
#if NETSTANDARD
|
||||
static MethodInfo modifyMethod = typeof(Instance).GetTypeInfo().GetMethod("Modified");
|
||||
@@ -48,33 +49,34 @@ public static class ResourceProxy
|
||||
|
||||
public static Type GetProxy(Type type)
|
||||
{
|
||||
|
||||
if (cache.ContainsKey(type))
|
||||
return cache[type];
|
||||
|
||||
// check if the type was made with code generation
|
||||
if (type.GetCustomAttribute<ResourceAttribute>(false) != null)
|
||||
lock (cacheLock)
|
||||
{
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
if (cache.ContainsKey(type))
|
||||
return cache[type];
|
||||
|
||||
if (!Codec.ImplementsInterface(type, typeof(IResource)))
|
||||
{
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
// check if the type was made with code generation
|
||||
if (type.GetCustomAttribute<ResourceAttribute>(false) != null)
|
||||
{
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
if (!Codec.ImplementsInterface(type, typeof(IResource)))
|
||||
{
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
|
||||
if (typeInfo.IsSealed || typeInfo.IsAbstract)
|
||||
throw new Exception("Sealed/Abastract classes can't be proxied.");
|
||||
if (typeInfo.IsSealed || typeInfo.IsAbstract)
|
||||
throw new Exception("Sealed/Abastract classes can't be proxied.");
|
||||
|
||||
var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
where p.CanWrite && p.SetMethod.IsVirtual && !p.SetMethod.IsFinal &&
|
||||
p.GetCustomAttribute<ExportAttribute>(false) != null
|
||||
select p;
|
||||
var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
where p.CanWrite && p.SetMethod.IsVirtual && !p.SetMethod.IsFinal &&
|
||||
p.GetCustomAttribute<ExportAttribute>(false) != null
|
||||
select p;
|
||||
|
||||
#else
|
||||
if (type.IsSealed)
|
||||
@@ -86,33 +88,34 @@ public static class ResourceProxy
|
||||
select p;
|
||||
|
||||
#endif
|
||||
var assemblyName = new AssemblyName("Esiur.Proxy.T." + type.Assembly.GetName().Name);// type.Namespace);
|
||||
assemblyName.Version = type.Assembly.GetName().Version;
|
||||
assemblyName.CultureInfo = type.Assembly.GetName().CultureInfo;
|
||||
//assemblyName.SetPublicKeyToken(null);
|
||||
var assemblyName = new AssemblyName("Esiur.Proxy.T." + type.Assembly.GetName().Name);// type.Namespace);
|
||||
assemblyName.Version = type.Assembly.GetName().Version;
|
||||
assemblyName.CultureInfo = type.Assembly.GetName().CultureInfo;
|
||||
//assemblyName.SetPublicKeyToken(null);
|
||||
|
||||
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
|
||||
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
|
||||
var typeName = "Esiur.Proxy.T." + type.FullName;// Assembly.CreateQualifiedName(assemblyName.FullName, "Esiur.Proxy.T." + type.FullName);
|
||||
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
|
||||
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
|
||||
var typeName = "Esiur.Proxy.T." + type.FullName;// Assembly.CreateQualifiedName(assemblyName.FullName, "Esiur.Proxy.T." + type.FullName);
|
||||
|
||||
var typeBuilder = moduleBuilder.DefineType(typeName,
|
||||
TypeAttributes.Public | TypeAttributes.Class, type);
|
||||
var typeBuilder = moduleBuilder.DefineType(typeName,
|
||||
TypeAttributes.Public | TypeAttributes.Class, type);
|
||||
|
||||
foreach (PropertyInfo propertyInfo in props)
|
||||
CreateProperty(propertyInfo, typeBuilder, type);
|
||||
foreach (PropertyInfo propertyInfo in props)
|
||||
CreateProperty(propertyInfo, typeBuilder, type);
|
||||
|
||||
|
||||
|
||||
#if NETSTANDARD
|
||||
var t = typeBuilder.CreateTypeInfo().AsType();
|
||||
cache.Add(type, t);
|
||||
return t;
|
||||
var t = typeBuilder.CreateTypeInfo().AsType();
|
||||
cache.Add(type, t);
|
||||
return t;
|
||||
#else
|
||||
|
||||
var t = typeBuilder.CreateType();
|
||||
cache.Add(type, t);
|
||||
return t;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public static Type GetProxy<T>()
|
||||
|
||||
Reference in New Issue
Block a user