public T ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <T> tReader) { return(tReader.ReadValue()); } var value = valueReader.DirectRead(); if (value is T tValue) { return(tValue); } var reader = RWHelper.CreateReader(value); if (reader is T tResult) { return(tResult); } if (reader != null && reader.As <TKey>() is T tResult2) { return(tResult2); } return(XConvert <T> .FromObject(value)); }
public DataSet ReadDataSet() { switch (state) { case ReadState.Table: var ds = new DataSet(); while (state != ReadState.Finish) { ds.Tables.Add(ReadDataTable()); } return(ds); case ReadState.Row: throw new NotSupportedException(); case ReadState.Value: return(XConvert <DataSet> .FromObject(DirectRead())); default: throw new ArgumentException("Finish"); } }
public T ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <T> ) { return(((IValueReader <T>)valueReader).ReadValue()); } return(XConvert.FromObject <T>(valueReader.DirectRead())); }
public T ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <T> ) { return(((IValueReader <T>)valueReader).ReadValue()); } var directValue = valueReader.DirectRead(); if (directValue is T || directValue == null) { return((T)directValue); } return(XConvert.FromObject <T>(directValue)); }
public TTask ReadValue(IValueReader valueReader) { var task = Run(); if (task is TTask ret) { return(ret); } return(XConvert <TTask> .FromObject(task)); async Task Run() { valueReader.DirectRead(); } }
public void ConvertTest() { const string str = "JXU3MkQ3JXU0RjFGJXU2NjJGJXU0RTE2JXU3NTRDJXU0RTBBJXU2NzAwJXU1RTA1JXU3Njg0JXU0RUJB"; AreEqual(XConvert <string> .Convert(XConvert <byte[]> .Convert(str)), str); AreEqual(XConvert <int> .Convert(XConvert <BigInteger> .Convert(int.MinValue)), int.MinValue); AreEqual(XConvert <BigInteger> .Convert(XConvert <byte[]> .Convert((BigInteger)ulong.MaxValue)), (BigInteger)ulong.MaxValue); AreEqual(XConvert <string> .FromObject(XConvert.ToObject(str, typeof(byte[]))), str); AreEqual(XConvert <int> .FromObject(XConvert.Cast(int.MinValue, typeof(BigInteger))), int.MinValue); AreEqual(XConvert <BigInteger> .FromObject(XConvert <byte[]> .FromObject((BigInteger)ulong.MaxValue)), (BigInteger)ulong.MaxValue); }
public DataTable ReadDataTable() { switch (state) { case ReadState.Table: var dt = new DataTable(); for (int i = 0; i < dbDataReader.FieldCount; i++) { dt.Columns.Add(dbDataReader.GetName(i), dbDataReader.GetFieldType(i)); } while (dbDataReader.Read()) { var row = dt.NewRow(); for (int i = 0; i < dbDataReader.FieldCount; i++) { row[i] = dbDataReader[i]; } dt.Rows.Add(row); } state = ReadState.Finish; return(dt); case ReadState.Row: throw new NotSupportedException(); case ReadState.Value: return(XConvert <DataTable> .FromObject(DirectRead())); default: if (dbDataReader.NextResult()) { goto case ReadState.Table; } throw new ArgumentException("Finish"); } }
public TimeSpan ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <TimeSpan> timeSpanReader) { return(timeSpanReader.ReadValue()); } object directValue = valueReader.DirectRead(); if (directValue is TimeSpan) { return((TimeSpan)directValue); } if (directValue is string) { return(TimeSpan.Parse((string)directValue)); } return(XConvert.FromObject <TimeSpan>(directValue)); }
public DateTimeOffset ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <DateTimeOffset> dateTimeOffsetReader) { return(dateTimeOffsetReader.ReadValue()); } object directValue = valueReader.DirectRead(); if (directValue is DateTimeOffset) { return((DateTimeOffset)directValue); } if (directValue is string) { return(DateTimeOffset.Parse((string)directValue)); } return(XConvert.FromObject <DateTimeOffset>(directValue)); }
public T ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <T> tReader) { return(tReader.ReadValue()); } if (valueReader is IValueReader <Type> typeReader) { return((T)typeReader.ReadValue()); } var value = valueReader.DirectRead(); if (value is string typeName && Type.GetType(typeName) is T result) { return(result); } return(XConvert <T> .FromObject(value)); }
public T ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <T> ) { return(((IValueReader <T>)valueReader).ReadValue()); } var value = valueReader.DirectRead(); if (value is T ret) { return(ret); } if (value is null) { return((T)(object)null); } return(XConvert.FromObject <T>(value)); }
public T ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <T> tReader) { return(tReader.ReadValue()); } if (valueReader is IValueReader <Assembly> assemblyReader) { return((T)assemblyReader.ReadValue()); } var value = valueReader.DirectRead(); if (value is string sssemblyString && Assembly.Load(sssemblyString) is T result) { return(result); } return(XConvert <T> .FromObject(value)); }
public T?ReadNullable <T>() where T : struct => XConvert.FromObject <T>(DirectRead());
public T ReadValue <T>(object obj) { return(XConvert.FromObject <T>(GetValue(obj))); }
public T ReadValue(IValueReader valueReader) { if (valueReader is IValueReader <T> tReader) { return(tReader.ReadValue()); } if (IsAssignableToMethodInfo && valueReader is IValueReader <MethodInfo> methodReader) { return((T)(object)methodReader.ReadValue()); } if (IsAssignableToFieldInfo && valueReader is IValueReader <MethodInfo> fieldReader) { return((T)(object)fieldReader.ReadValue()); } if (IsAssignableToPropertyInfo && valueReader is IValueReader <PropertyInfo> propertyReader) { return((T)(object)propertyReader.ReadValue()); } if (IsAssignableToConstructorInfo && valueReader is IValueReader <ConstructorInfo> constructorReader) { return((T)(object)constructorReader.ReadValue()); } if (IsAssignableToEventInfo && valueReader is IValueReader <EventInfo> eventReader) { return((T)(object)eventReader.ReadValue()); } var value = valueReader.DirectRead(); if (value is null) { return(null); } if (RWHelper.CreateReader(value, false) is IDataReader <string> infoReader) { var declaringType = ValueInterface <Type> .ReadValue(infoReader["DeclaringType"]); var name = ValueInterface <string> .ReadValue(infoReader["Name"]); if (name == ConstructorName || IsAssignableToConstructorInfo) { var parameterTypes = ValueInterface <Type[]> .ReadValue(infoReader["ParameterTypes"]); var constructor = declaringType.GetConstructor(parameterTypes); if (constructor != null && constructor is T ret) { return(ret); } } else if (name == IndexerName) { var parameterTypes = ValueInterface <Type[]> .ReadValue(infoReader["ParameterTypes"]); var propertyType = ValueInterface <Type> .ReadValue(infoReader["PropertyType"]); var indexer = declaringType.GetProperty(parameterTypes); if (indexer != null && indexer.PropertyType == propertyType && indexer is T ret) { return(ret); } } else if (IsAssignableToFieldInfo) { var fieldType = ValueInterface <Type> .ReadValue(infoReader["FieldType"]); var field = declaringType.GetField(name); if (field != null && field.FieldType == fieldType && field is T ret) { return(ret); } } else if (IsAssignableToPropertyInfo) { var propertyType = ValueInterface <Type> .ReadValue(infoReader["PropertyType"]); var property = declaringType.GetProperty(name); if (property != null && property.PropertyType == propertyType && property is T ret) { return(ret); } } else if (IsAssignableToEventInfo) { var eventHandlerType = ValueInterface <Type> .ReadValue(infoReader["EventHandlerType"]); var @event = declaringType.GetEvent(name); if (@event != null && @event.EventHandlerType == eventHandlerType && @event is T ret) { return(ret); } } else if (IsAssignableToMethodInfo) { goto Method; } else { var member = declaringType.GetMember(name, Flags); if (member.Length > 0) { switch (member[0].MemberType) { case MemberTypes.Event: { var eventHandlerType = ValueInterface <Type> .ReadValue(infoReader["EventHandlerType"]); if (member[0] is EventInfo @event && @event.EventHandlerType == eventHandlerType && @event is T ret) { return(ret); } } break; case MemberTypes.Field: { var fieldType = ValueInterface <Type> .ReadValue(infoReader["FieldType"]); if (member[0] is FieldInfo field && field.FieldType == fieldType && field is T ret) { return(ret); } } break; case MemberTypes.Property: { var propertyType = ValueInterface <Type> .ReadValue(infoReader["PropertyType"]); if (member[0] is PropertyInfo property && property.PropertyType == propertyType && property is T ret) { return(ret); } } break; case MemberTypes.Method: goto Method; default: throw new NotSupportedException(); } } } NoFound: throw new MissingMemberException(declaringType.FullName, name); Method: { var parameterTypes = ValueInterface <Type[]> .ReadValue(infoReader["ParameterTypes"]); var returnType = ValueInterface <Type> .ReadValue(infoReader["ReturnType"]); var method = declaringType.GetMethod(name, Flags, Type.DefaultBinder, parameterTypes, null); if (method != null && method.ReturnType == returnType && method is T ret) { return(ret); } goto NoFound; } } if (value is string memberName) { try { if (Type.GetType(memberName) is T ret) { return(ret); } } catch { } } return(XConvert <T> .FromObject(value)); }