//public DTypeInfo GetInterface (string iface) public DArgumentInfo[] GetFields(string iface) { DTypeInfo dti = new DTypeInfo(iface); Type declType = dti.ToType(); List <DArgumentInfo> fields = new List <DArgumentInfo>(); foreach (FieldInfo fi in declType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { fields.Add(new DArgumentInfo(fi.Name, new DTypeInfo(fi.FieldType.FullName))); } //return dti; return(fields.ToArray()); }
/// <summary> /// Teach ioDriver how to create instances and read dimensional data from type T. /// <seealso cref="TeachCoord{T}(int,FuncConstruct{T},FuncGetDim{T}[])"/> /// </summary> /// <typeparam name="T">Data type</typeparam> /// <param name="_constructs">Dictionary of constructor functions, keyed by number of dimensions</param> /// <param name="_getFuncs">Params of get dimension functions</param> public static void TeachCoord <T>(Dictionary <int, FuncConstruct <T> > _constructs, params FuncGetDim <T>[] _getFuncs) { var maxDimCount = 0; foreach (var dimCount in _constructs.Keys) { if (dimCount > _getFuncs.Length) { throw new Exception("Received constructor dim count greater than number of get dims functions."); } if (maxDimCount < dimCount) { maxDimCount = dimCount; } } if (maxDimCount < _getFuncs.Length) { throw new Exception( "Did not receive constructor that handles all provided dim funcs. Max Constructor Dim: " + maxDimCount + " Get Dim function count: " + _getFuncs.Length); } // Teach Constructors foreach (var pair in _constructs) { DTypeInfo <T> .SetConstruct(pair.Value, pair.Key); } // Teach dimension data retrieval DTypeInfo <T> .GetDimsFunc = new FuncGetDim <T> [_getFuncs.Length + 1]; _getFuncs.CopyTo(DTypeInfo <T> .GetDimsFunc, 1); if (m_TaughtTypes == null) { ioDriver.Init(); } if (!m_TaughtTypes.ContainsKey(typeof(T))) { m_TaughtTypes.Add(typeof(T), new HashSet <TeachType>()); } m_TaughtTypes[typeof(T)].Add(TeachType.Coord); }
//public DTypeInfo GetInterface (string iface) public DArgumentInfo[] GetFields (string iface) { DTypeInfo dti = new DTypeInfo (iface); Type declType = dti.ToType(); List<DArgumentInfo> fields = new List<DArgumentInfo>(); foreach (FieldInfo fi in declType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)) fields.Add (new DArgumentInfo(fi.Name, new DTypeInfo(fi.FieldType.FullName))); //return dti; return fields.ToArray(); }
public DArgumentInfo(string name, DTypeInfo argType, DArgumentDirection direction) { this.Name = name; this.ArgType = argType; this.Direction = direction; }
public DArgumentInfo(string name, DTypeInfo argType) : this(name, argType, DArgumentDirection.In) { }
public DArgumentInfo(DTypeInfo argType) : this(String.Empty, argType) { }
internal object Deserialize(Stream serializationStream) { object result = null; try { using (MemoryStream ms = new MemoryStream()) { int b = serializationStream.ReadByte(); if (b == 0) { #region Get assemblies // get number of assemblies b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int asmCounter = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); for (int i = 0; i < asmCounter; i++) { // asm id b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int asmId = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); // asm name b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } string asm = Encoding.UTF8.GetString(ms.ToArray()); ms.SetLength(0); mAsmIdVsAssemblyName.Add(asmId, new AssemblyName(asm)); } #endregion #region Proceed types // get number of types b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int typeCounter = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); for (int i = 0; i < typeCounter; i++) { DTypeInfo typeInfo = new DTypeInfo(); // typeId b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } typeInfo.TypeId = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); mTypeIdVsTypeInfo.Add(typeInfo.TypeId, typeInfo); ms.SetLength(0); // type name b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } typeInfo.TypeName = Encoding.UTF8.GetString(ms.ToArray()); ms.SetLength(0); // read asmId { int index1 = typeInfo.TypeName.IndexOf("{"); int index2 = typeInfo.TypeName.IndexOf("}"); if (index1 >= 0) { index1++; typeInfo.AssemblyId = int.Parse(typeInfo.TypeName.Substring(index1, index2 - index1)); index1--; typeInfo.TypeName = typeInfo.TypeName.Substring(0, index1); } } b = serializationStream.ReadByte(); if (b == 1) { typeInfo.IsArray = true; } typeInfo.IsNested = typeInfo.TypeName.Contains("+"); // read generic info b = serializationStream.ReadByte(); if (b == 1) { typeInfo.IsGenericType = true; // IsGenericDeclaration? b = serializationStream.ReadByte(); if (b == 1) { typeInfo.IsGenericDeclaration = true; } if (!typeInfo.IsGenericDeclaration) { // típus azonosítók kiolvasása List<int> genericParameterTypes = new List<int>(); int indexOfChar = typeInfo.TypeName.IndexOf("[") + 1; int origIndexOfChar = indexOfChar - 1; string idChar = typeInfo.TypeName.Substring(indexOfChar, 1); string idRefNumber = string.Empty; while (!idChar.Equals("]")) { if (mNumbers.Contains(idChar)) { idRefNumber = idRefNumber + idChar; } else if (idChar.Equals(";")) { genericParameterTypes.Add(int.Parse(idRefNumber)); idRefNumber = string.Empty; } indexOfChar++; idChar = typeInfo.TypeName.Substring(indexOfChar, 1); } genericParameterTypes.Add(int.Parse(idRefNumber)); typeInfo.GenericParameterTypeIds = genericParameterTypes; typeInfo.TypeName = string.Format("{0}*{1}", typeInfo.TypeName.Substring(0, origIndexOfChar), typeInfo.TypeName.Substring(indexOfChar + 1)); } } // number of deserializable fields b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } typeInfo.DeserializableFieldNumber = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); if (typeInfo.IsNested) { int index = 1; string refIdChar = typeInfo.TypeName.Substring(index, 1); string refId = string.Empty; while (mNumbers.Contains(refIdChar)) { refId = refId + refIdChar; index++; refIdChar = typeInfo.TypeName.Substring(index, 1); } typeInfo.DeclaredTypeId = int.Parse(refId); typeInfo.TypeName = typeInfo.TypeName.Substring(index + 2); // levágom: )+ } } // resolve types for (int i = 0; i < typeCounter; i++) { DTypeInfo ti = mTypeIdVsTypeInfo[i]; if (ti.Type == null) { ti.ResolveType(this); } } #endregion #region Build instances // build instances, number of items b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int numberOfItems = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); // create proxies for (int i = 1; i <= numberOfItems; i++) { #region InstanceId // instanceId b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } long instanceId = long.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); #endregion #region TypeId // typeId b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int typeId = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); #endregion #region Create proxy // create proxy DTypeInstanceProxy proxy = mTypeIdVsTypeInfo[typeId].Clone() as DTypeInstanceProxy; proxy.InstanceId = instanceId; mInstanceIdVsInstanceProxy.Add(instanceId, proxy); #region ArrayKeys b = serializationStream.ReadByte(); if (b == 1) { // has array keys b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int arrayKeyLen = ms.Length == 0 ? -1 : int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); proxy.ArrayKeysRefIds = new List<int>(arrayKeyLen); for (int index = 0; index < arrayKeyLen; index++) { b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int arrayKeyProxyId = ms.Length == 0 ? -1 : int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); proxy.ArrayKeysRefIds.Add(arrayKeyProxyId); } } #endregion b = serializationStream.ReadByte(); proxy.IsArrayDimensionRepresentation = (b == 1 ? true : false); #region Array int arrayItemNumber = 0; if (proxy.Type.IsArray || proxy.IsArrayDimensionRepresentation || typeof(ISerializable).IsAssignableFrom(proxy.Type)) { // array type // number of array items b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } arrayItemNumber = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); if (!proxy.Type.Equals(typeof(byte[]))) { // byte tömböt máshogy kezelem for (int index = 0; index < arrayItemNumber; index++) { b = serializationStream.ReadByte(); if (b == 0) { proxy.ArrayItemsRefIds.Add(-1); } else { while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } proxy.ArrayItemsRefIds.Add(int.Parse(Encoding.UTF8.GetString(ms.ToArray()))); ms.SetLength(0); } } } } #endregion #region Field items // has field items? b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int fieldItemNumber = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); for (int index = 0; index < fieldItemNumber; index++) { // read field name b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } string fieldName = Encoding.UTF8.GetString(ms.ToArray()); ms.SetLength(0); // read reference value (instanceId) b = serializationStream.ReadByte(); if (b == 0) { proxy.FieldsVsInstanceIds.Add(fieldName, null); } else { while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int fieldRefId = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); proxy.FieldsVsInstanceIds.Add(fieldName, fieldRefId); } } #endregion #region Instance values // read instance value Type reflectionType = proxy.Type; if (mSystemByteTypes.Contains(reflectionType)) { ms.WriteByte((byte)serializationStream.ReadByte()); if (reflectionType.Equals(typeof(byte))) { proxy.InstanceValue = ms.ToArray()[0]; proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(byte?))) { proxy.InstanceValue = new Nullable<byte>(ms.ToArray()[0]); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(bool?)) || reflectionType.Equals(typeof(bool))) { if (ms.ToArray()[0] == 1) { proxy.InstanceValue = true; } else { proxy.InstanceValue = false; } proxy.IsConstructed = true; } } else if (proxy.Type.Equals(typeof(byte[]))) { byte[] bytes = null; if (arrayItemNumber == 0) { bytes = new byte[0]; } else { bytes = new byte[arrayItemNumber]; serializationStream.Read(bytes, 0, bytes.Length); } proxy.InstanceValue = bytes; proxy.IsConstructed = true; } else { b = serializationStream.ReadByte(); if (b != 0) { while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } if (reflectionType.IsEnum) { proxy.InstanceValue = Enum.ToObject(reflectionType, Convert.ChangeType(Encoding.UTF8.GetString(ms.ToArray()), reflectionType.GetEnumUnderlyingType())); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(short))) { proxy.InstanceValue = short.Parse(Encoding.UTF8.GetString(ms.ToArray())); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(ushort))) { proxy.InstanceValue = ushort.Parse(Encoding.UTF8.GetString(ms.ToArray())); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(int))) { proxy.InstanceValue = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(uint))) { proxy.InstanceValue = uint.Parse(Encoding.UTF8.GetString(ms.ToArray())); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(long))) { proxy.InstanceValue = long.Parse(Encoding.UTF8.GetString(ms.ToArray())); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(ulong))) { proxy.InstanceValue = ulong.Parse(Encoding.UTF8.GetString(ms.ToArray())); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(short?))) { proxy.InstanceValue = new Nullable<short>(short.Parse(Encoding.UTF8.GetString(ms.ToArray()))); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(ushort?))) { proxy.InstanceValue = new Nullable<ushort>(ushort.Parse(Encoding.UTF8.GetString(ms.ToArray()))); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(int?))) { proxy.InstanceValue = new Nullable<int>(int.Parse(Encoding.UTF8.GetString(ms.ToArray()))); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(uint?))) { proxy.InstanceValue = new Nullable<uint>(uint.Parse(Encoding.UTF8.GetString(ms.ToArray()))); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(long?))) { proxy.InstanceValue = new Nullable<long>(long.Parse(Encoding.UTF8.GetString(ms.ToArray()))); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(ulong?))) { proxy.InstanceValue = new Nullable<ulong>(ulong.Parse(Encoding.UTF8.GetString(ms.ToArray()))); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(float))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = float.Parse(Encoding.UTF32.GetString(bytes), System.Globalization.CultureInfo.InvariantCulture); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(double))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = double.Parse(Encoding.UTF32.GetString(bytes), System.Globalization.CultureInfo.InvariantCulture); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(decimal))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = decimal.Parse(Encoding.UTF32.GetString(bytes), System.Globalization.CultureInfo.InvariantCulture); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(float?))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = new Nullable<float>(float.Parse(Encoding.UTF32.GetString(bytes), System.Globalization.CultureInfo.InvariantCulture)); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(double?))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = new Nullable<double>(double.Parse(Encoding.UTF32.GetString(bytes), System.Globalization.CultureInfo.InvariantCulture)); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(decimal?))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = new Nullable<decimal>(decimal.Parse(Encoding.UTF32.GetString(bytes), System.Globalization.CultureInfo.InvariantCulture)); proxy.IsConstructed = true; } else if (typeof(Type).IsAssignableFrom(reflectionType)) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = TypeHelper.GetTypeFromString(Encoding.UTF32.GetString(bytes), this.TypeLookupMode, this.FindNewestTypeVersion, true, false); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(string))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = Encoding.UTF32.GetString(bytes); proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(char))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = Encoding.UTF32.GetChars(bytes)[0]; proxy.IsConstructed = true; } else if (reflectionType.Equals(typeof(char?))) { int length = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); byte[] bytes = new byte[length]; serializationStream.Read(bytes, 0, bytes.Length); proxy.InstanceValue = new Nullable<char>(Encoding.UTF32.GetChars(bytes)[0]); proxy.IsConstructed = true; } } } ms.SetLength(0); #endregion #endregion } // read null representation proxy b = serializationStream.ReadByte(); if (b == 1) { // van null b = serializationStream.ReadByte(); while (b > 0) { ms.WriteByte((byte)b); b = serializationStream.ReadByte(); } int nullProxyId = int.Parse(Encoding.UTF8.GetString(ms.ToArray())); ms.SetLength(0); mInstanceIdVsInstanceProxy[nullProxyId].IsConstructed = true; } // construct values result = mInstanceIdVsInstanceProxy[1].Construct(this); #endregion } else if (b != 1) { throw new FormatException("Invalid first char in content stream."); } } } finally { mAsmIdVsAssemblyName.Clear(); mTypeIdVsTypeInfo.Clear(); mInstanceIdVsInstanceProxy.Clear(); } return result; }