2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00

Generics workaround

This commit is contained in:
2022-06-05 02:06:59 +03:00
parent f1584b348d
commit 499f1e37d2
7 changed files with 89 additions and 78 deletions

View File

@ -284,7 +284,17 @@ public static class Codec
}
else
{
if (type.IsGenericType)
if (Codec.ImplementsInterface(type, typeof(IResource)))
{
var (hdr, data) = DataSerializer.ResourceComposer(valueOrSource, connection);
return TransmissionType.Compose(hdr, data);
}
else if (Codec.ImplementsInterface(type, typeof(IRecord)))
{
var (hdr, data) = DataSerializer.RecordComposer(valueOrSource, connection);
return TransmissionType.Compose(hdr, data);
}
else if (type.IsGenericType)
{
var genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(List<>))
@ -326,17 +336,7 @@ public static class Codec
return TransmissionType.Compose(hdr, data);
//}
}
else if (Codec.ImplementsInterface(type, typeof(IResource)))
{
var (hdr, data) = DataSerializer.ResourceComposer(valueOrSource, connection);
return TransmissionType.Compose(hdr, data);
}
else if (Codec.ImplementsInterface(type, typeof(IRecord)))
{
var (hdr, data) = DataSerializer.RecordComposer(valueOrSource, connection);
return TransmissionType.Compose(hdr, data);
}
}
else if (type.IsEnum)
{
var (hdr, data) = DataSerializer.EnumComposer(valueOrSource, connection);

View File

@ -168,13 +168,11 @@ namespace Esiur.Data
public RepresentationType?[] SubTypes = new RepresentationType[3];
public static RepresentationType? FromType(Type type)//, bool forceNullable = false)
public static RepresentationType? FromType(Type type)
{
var nullable = false;// = forceNullable;
//if (!forceNullable)
//{
var nullable = false;
var nullType = System.Nullable.GetUnderlyingType(type);
if (nullType != null)
@ -182,9 +180,30 @@ namespace Esiur.Data
type = nullType;
nullable = true;
}
//}
if (type.IsGenericType)
if (type == typeof(IResource))
return new RepresentationType(RepresentationTypeIdentifier.Resource, nullable);
else if (type == typeof(IRecord) || type == typeof(Record))
return new RepresentationType(RepresentationTypeIdentifier.Record, nullable);
else if (type == typeof(Map<object, object>))
return new RepresentationType(RepresentationTypeIdentifier.Map, nullable);
else if (Codec.ImplementsInterface(type, typeof(IResource)))
{
return new RepresentationType(
RepresentationTypeIdentifier.TypedResource,
nullable,
TypeTemplate.GetTypeGuid(type)
);
}
else if (Codec.ImplementsInterface(type, typeof(IRecord)))
{
return new RepresentationType(
RepresentationTypeIdentifier.TypedRecord,
nullable,
TypeTemplate.GetTypeGuid(type)
);
}
else if (type.IsGenericType)
{
var genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(List<>))
@ -327,28 +346,6 @@ namespace Esiur.Data
}
}
else if (type == typeof(IResource))
return new RepresentationType(RepresentationTypeIdentifier.Resource, nullable);
else if (type == typeof(IRecord) || type == typeof(Record))
return new RepresentationType(RepresentationTypeIdentifier.Record, nullable);
else if (type == typeof(Map<object, object>))
return new RepresentationType(RepresentationTypeIdentifier.Map, nullable);
else if (Codec.ImplementsInterface(type, typeof(IResource)))
{
return new RepresentationType(
RepresentationTypeIdentifier.TypedResource,
nullable,
TypeTemplate.GetTypeGuid(type)
);
}
else if (Codec.ImplementsInterface(type, typeof(IRecord)))
{
return new RepresentationType(
RepresentationTypeIdentifier.TypedRecord,
nullable,
TypeTemplate.GetTypeGuid(type)
);
}
else if (type.IsEnum)
{
return new RepresentationType(RepresentationTypeIdentifier.Enum, nullable, TypeTemplate.GetTypeGuid(type));

View File

@ -190,20 +190,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
set;
}
//[Attribute]
//public virtual uint Timeout
//{
// get;
// set;
//}
//[Attribute]
//public virtual uint Clock
//{
// get;
// set;
//}
[Attribute]
public virtual uint MaxPost
{

View File

@ -158,7 +158,7 @@ public class TypeTemplate
}
public static Guid GetTypeGuid(Type type) => GetTypeGuid(type.FullName);
public static Guid GetTypeGuid(Type type) => GetTypeGuid(GetTypeClassName(type));
public static Guid GetTypeGuid(string typeName)
{
@ -174,6 +174,11 @@ public class TypeTemplate
return GetDistributedTypes(type.GetElementType());
else if (type.IsEnum)
return new Type[] { type };
else if (Codec.ImplementsInterface(type, typeof(IRecord))
|| Codec.ImplementsInterface(type, typeof(IResource)))
{
return new Type[] { type };
}
else if (type.IsGenericType)
{
var genericType = type.GetGenericTypeDefinition();
@ -199,11 +204,7 @@ public class TypeTemplate
return rt.ToArray();
}
}
else if (Codec.ImplementsInterface(type, typeof(IRecord))
|| Codec.ImplementsInterface(type, typeof(IResource)))
{
return new Type[] { type };
}
return new Type[0];
}
@ -354,6 +355,22 @@ public class TypeTemplate
return type.Name + "?";
}
public static string GetTypeClassName(Type type, string separator = ".")
{
if (type.IsGenericType)
{
var index = type.Name.IndexOf("`");
var name = $"{type.Namespace}{separator}{((index > -1) ? type.Name.Substring(0, index) : type.Name)}Of";
foreach (var t in type.GenericTypeArguments)
name += GetTypeClassName(t, "_");
return name;
}
else
return $"{type.Namespace}{separator}{type.Name}";
}
public TypeTemplate(Type type, bool addToWarehouse = false)
{
if (Codec.InheritsClass(type, typeof(DistributedResource)))
@ -377,7 +394,7 @@ public class TypeTemplate
DefinedType = type;
className = type.FullName;
className = GetTypeClassName(type);
// set guid
classId = GetTypeGuid(className);
@ -615,7 +632,6 @@ public class TypeTemplate
ft.MethodInfo = mi;
functions.Add(ft);
};
@ -761,7 +777,7 @@ public class TypeTemplate
{
// find the first parent type that implements IResource
var ParentDefinedType = ResourceProxy.GetBaseType(type.BaseType);
var parentId = GetTypeGuid(ParentDefinedType.FullName);
var parentId = GetTypeGuid(ParentDefinedType);
b.AddUInt8((byte)(0x80 | (byte)templateType))
.AddGuid(classId)

View File

@ -1,3 +1,2 @@

1- Bin
2- clean
1- Generic Records/Resources