123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821 |
- //**************************************
- // Name: The Return of the Variant (ver. 6)
- // Description:A Variant structure written in C#, ideal for VB.NET 2005 or C# 2002/2003/2005 developers who miss the old (and, in the eyes of many, ugly) VB6 Variant. A Variant is a type that contains any type of variable and can be worked with "automagically" without heavy typecasting procedures. (System.Object is not a variant equivalent because you cannot do things like addition with it.)
- // One important difference about this structure: It becomes what you do to it. So if the value is a string and you add an integer of 12, the value becomes an integer. If you want it to remain the same, you should typecast first--for example, add "12" rather than (int)12.
- // Changes for v2:
- // - All type properties ("String", etc.) are now private.
- // - Added implicit/explicit custom operators, so typecasts now work naturally. For example:
- // string myString = (string)myVariant; // no longer myVariant.String ...
- // float myFloat = (float)myVariant; // no longer myVariant.Float ...
- // bool myBool = (bool)myVariant; // no longer myVariant.Boolean ....
- // Changes for v3: Quite a few. Added DateTime support. Implemented IConvertible. Changed all the private type name properties to public "To[type]()" methods. Changed the naming convention to conform to IConvertible.
- // Changes to v3.1: Removed many unnecessary if statements. Made some documentation updates.
- // Changes to v4: Removed the "Type becomes what you do with it" rule for numeric operations. Example: adding an integer of 1 to a float of 1.1 results in a float of 2.1 rather than an integer of 2. Changed operator overloads to automatically adjust the type if the operation causes the resulting value to extend numerically beyond the value of the type. For example, if an integer (Int32) operation results in a value than can fit into an integer, the value is changed to a long (Int64).
- // Changes to v5: Changed operator overloads so that if auto-converting to a compatible type, the new type is not smaller than the previous type; Made changes to CBln() to default to True if the object is a non-empty string; Added lots of documentation. Change to v5.1... it's IsNumberable, not IsNumerable. :) Also, To[Type]() should default to the specified type. And SmartMath() method name changed to VariantArithmetic(). Changes in v6... Added TimeSpan type support; added IsDate, IsTimeSpan; updated operator overloads, including equality (==).
- // By: Jon Davis
- //
- //
- // Inputs:// Here is an example of use:
- // start with a string of "1"
- // Variant da = new Variant("1");
- // add an integer of 2, becomes 3
- // da += 2;
- // add a float of 1.3, becomes 4.3
- // da += 1.3F;
- // add a string, becomes "4.3wow"
- // da += "wow";
- // writes "4.3wow"
- // Console.WriteLine(da);
- //
- // Returns:None
- //
- // Assumes:None
- //
- // Side Effects:Note that as with all typecasting rules, the Variant can be
- // dangerous if you are unsure of what you are doing. In this case, the
- // Variant class remains strongly typed, while making the most of the CLR's
- // intrinsic type conversion features. Conversion of a float (1.5) to an
- // integer, for instance, will of course result in data loss. In some cases,
- // conversion will simply fail, such as extracting the integer value of the
- // string "wow".
- // This code is copyrighted and has limited warranties.
- // Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.2854/lngWId.10/qx/vb/scripts/ShowCode.htm
- // for details.
- //**************************************
- using System;
- namespace FastReport
- {
- /// <summary>
- /// A strongly typed object that readily casts an intrinsic
- /// object to the other intrinsic types when possible.
- /// </summary>
- /// <remarks>
- /// <para>The Variant class is an intrinsic object container structure
- /// inspired by Visual Basic 6.0's Variant. The key features
- /// of a Variant class include the ability to perform typecasts and
- /// arithmetic between types that are not normally considered compatible.
- /// For example, if a Variant class contains a string describing a
- /// number, such as "1.1", then floating point arithmetic can be
- /// performed on it.</para>
- /// <para>Variants are normally considered dangerous because they
- /// strip away the effectiveness of type safety, which is the
- /// reason why the Visual Basic 6.0 Variant was left out of
- /// Visual Basic .NET. However, this implementation restores the
- /// Variant structure, both as a proof of concept and as a
- /// restoration of the utility and positive merits of the Variant
- /// where it can be used responsibly.</para>
- /// </remarks>
- #if READONLY_STRUCTS
- public readonly struct Variant
- #else
- public struct Variant
- #endif
- : IConvertible, IComparable {
- /// <summary>
- /// Creates a strongly typed object that readily casts a primitive
- /// object to the other primitive types when possible.
- /// </summary>
- public Variant(object value) {
- if (value == DBNull.Value)
- value = null;
- if (value != null && value.GetType() == typeof(Variant))
- _value = ((Variant)value).Value;
- else
- _value = value;
- }
- private readonly object _value;
- /// <summary>
- /// The actual value being stored in its original <see cref="System.Type"/>,
- /// returned as an <see cref="Object"/>.
- /// </summary>
- public object Value {
- get {
- return this._value;
- }
- }
- /// <summary>
- /// The <see cref="System.Type"/> of the <see cref="Value"/> property.
- /// </summary>
- public Type Type {
- get {
- if (_value == null) return null;
- return _value.GetType();
- }
- }
- /// <summary>
- /// Returns the <see cref="System.TypeCode"/> for this instance.
- /// </summary>
- /// <returns>The enumerated constant that is the <see cref="System.TypeCode"/>
- /// of the class or value type that implements this interface.</returns>
- public TypeCode GetTypeCode() {
- return System.Type.GetTypeCode(this.Type);
- }
- /// <summary>
- /// Returns the string equivalent of the <see cref="Value"/> property.
- /// </summary>
- private string String {
- get {
- return _value.ToString();
- }
- }
- /// <summary>
- /// Attempts to convert or typecast to the specified type.
- /// </summary>
- /// <param name="type">The type to convert or cast to.</param>
- /// <returns>The object after typecasting.</returns>
- public object ToType(Type type) {
- return ToType(type, false, null);
- }
- /// <summary>
- /// Attempts to convert or typecast to the specified type.
- /// </summary>
- /// <param name="type">The type to convert or cast to.</param>
- /// <param name="provider">An <see cref="IFormatProvider"/>
- /// interface implementation that supplies culture-specific formatting information.</param>
- /// <returns>The object after typecasting.</returns>
- public object ToType(Type type, IFormatProvider provider) {
- return ToType(type, false, provider);
- }
- private object ToType(Type type, bool nomap) {
- return ToType(type, nomap, null);
- }
- private object ToType(Type type, bool nomap, IFormatProvider formatProvider) {
- if (type == this.Type) return _value;
- if (!nomap && formatProvider == null) {
- return TypeMap(type);
- }
- try {
- if (formatProvider != null) {
- return System.Convert.ChangeType(_value, type, formatProvider);
- } else {
- return System.Convert.ChangeType(_value, type);
- }
- } catch {}
- System.Reflection.MethodInfo[] mis = this.Type.GetMethods();
- foreach (System.Reflection.MethodInfo mi in mis) {
- if (mi.Name.ToLower() == "to" + type.Name.ToLower()) {
- try {
- if (formatProvider != null && mi.GetParameters().Length == 1) {
- if (mi.GetParameters()[0].ParameterType.IsSubclassOf(typeof(IFormatProvider)))
- return mi.Invoke(_value, new object[] {formatProvider});
- }
- return mi.Invoke(_value, new object[] {});
- } catch {}
- }
- }
- if (!nomap) return TypeMap(type);
- throw new InvalidCastException(
- "Cannot determine conversion method.");
- }
- private object TypeMap(Type type) {
- Type tt = type;
- if (tt == typeof(System.Boolean))
- return this.ToBoolean();
- if (tt == typeof(System.String))
- return this.ToString();
- if (tt == typeof(System.Char))
- return this.ToChar();
- if (tt == typeof(System.Byte))
- return this.ToByte();
- if (tt == typeof(System.Int16))
- return this.ToInt16();
- if (tt == typeof(System.Int32))
- return this.ToInt32();
- if (tt == typeof(System.Int64))
- return this.ToInt64();
- if (tt == typeof(System.SByte))
- return this.ToSByte();
- if (tt == typeof(System.UInt16))
- return this.ToUInt16();
- if (tt == typeof(System.UInt32))
- return this.ToUInt32();
- if (tt == typeof(System.UInt64))
- return this.ToUInt64();
- if (tt == typeof(System.Single))
- return this.ToSingle();
- if (tt == typeof(System.Double))
- return this.ToDouble();
- if (tt == typeof(System.Decimal))
- return this.ToDecimal();
- if (tt == typeof(System.DateTime))
- return this.ToDateTime();
- if (tt == typeof(System.TimeSpan))
- return this.ToTimeSpan();
- if (tt == typeof(System.DateTimeOffset))
- return this.ToDateTimeOffset();
- return ToType(type, true);
- }
- /// <summary>
- /// Returns true if the <see cref="Value"/> property implements <see cref="IConvertible"/>
- /// </summary>
- public bool ImplementsIConvertible {
- get {
- return Type.IsSubclassOf(typeof(IConvertible));
- }
- }
- /// <summary>
- /// Returns true if the <see cref="Value"/> property
- /// is a numeric intrinsic value.
- /// </summary>
- public bool IsNumeric {
- get {
- Type tt = this.Type;
- return (tt == typeof(byte) ||
- tt == typeof(sbyte) ||
- tt == typeof(short) ||
- tt == typeof(int) ||
- tt == typeof(long) ||
- tt == typeof(ushort) ||
- tt == typeof(uint) ||
- tt == typeof(ulong) ||
- tt == typeof(float) ||
- tt == typeof(double) ||
- tt == typeof(decimal));
- }
- }
- private static char decimalSep = ((float)1.1).ToString().ToCharArray()[1];
- /// <summary>
- /// Returns true if the <see cref="Value"/> property
- /// is a numeric intrinsic value or else can be parsed into
- /// a numeric intrinsic value.
- /// </summary>
- public bool IsNumberable {
- get {
- if (IsNumeric) return true;
- Type tt = this.Type;
- if (tt == typeof(bool)) return true;
- if (tt == typeof(string) ||
- tt == typeof(char)) {
- try {
- foreach (char c in this.ToString().ToCharArray()) {
- if (!char.IsDigit(c) && c != decimalSep) return false;
- }
- //double d = (this.ToDouble() + (double)0.1);
- return true;
- } catch {
- return false;
- }
- }
- return false;
- }
- }
- /// <summary>
- /// Returns true if the value is a date or can be parsed into a date.
- /// </summary>
- public bool IsDate {
- get {
- Type tt = this.Type;
- if (tt == typeof(DateTime)) return true;
- if (tt == typeof(string)) {
- DateTime dt = new DateTime();
- if (DateTime.TryParse(this.ToString(), out dt))
- return true;
- }
- return false;
- }
- }
- /// <summary>
- /// Returns true if the value is a TimeSpan.
- /// </summary>
- public bool IsTimeSpan {
- get {
- return this.Type == typeof(TimeSpan);
- }
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Boolean"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent <see cref="System.Boolean"/> value.
- /// </summary>
- public bool ToBoolean() {
- if (_value == null) return false;
- if (this.Type == typeof(System.Boolean))
- return (bool)_value;
- return CBln(_value);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Boolean"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent <see cref="System.Boolean"/> value using the specified culture-specific
- /// formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public bool ToBoolean(IFormatProvider formatProvider) {
- return (bool)ToType(typeof(bool), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Byte"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 8-bit unsigned integer.
- /// </summary>
- public byte ToByte() {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.Byte))
- return (byte)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return byte.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (Byte)1;
- else
- return (Byte)0;
- if (tt == typeof(System.DateTime))
- return (byte)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (byte)((TimeSpan)_value).Ticks;
- return (byte)ToType(typeof(byte), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Byte"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 8-bit unsigned integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public byte ToByte(IFormatProvider formatProvider) {
- return (byte)ToType(typeof(byte), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Int16"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 16-bit signed integer.
- /// </summary>
- public short ToInt16() {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.Int16))
- return (short)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return short.Parse(this._value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (short)1;
- else
- return (short)0;
- if (tt == typeof(System.DateTime))
- return (short)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (short)((TimeSpan)_value).Ticks;
- return (short)ToType(typeof(short), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Boolean"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent 16-bit signed integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public short ToInt16(IFormatProvider formatProvider) {
- return (short)ToType(typeof(short), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Int32"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 32-bit signed integer.
- /// </summary>
- public int ToInt32() {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.Int32))
- return (int)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return int.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (int)1;
- else
- return (int)0;
- if (tt == typeof(System.DateTime))
- return (int)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (int)((TimeSpan)_value).Ticks;
- return (int)ToType(typeof(int), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Int32"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 32-bit signed integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public int ToInt32(IFormatProvider formatProvider) {
- return (int)ToType(typeof(int), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Int64"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 64-bit signed integer.
- /// </summary>
- public long ToInt64() {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.Int64))
- return (long)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return long.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (long)1;
- else
- return (long)0;
- if (tt == typeof(System.DateTime))
- return (long)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (long)((TimeSpan)_value).Ticks;
- return (long)ToType(typeof(long), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Int64"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 64-bit signed integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public long ToInt64(IFormatProvider formatProvider) {
- return (long)ToType(typeof(long), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Double"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent double-precision floating-point number.
- /// </summary>
- public double ToDouble() {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.Double))
- return (double)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return double.Parse(
- _value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (double)1;
- else
- return (double)0;
- if (tt == typeof(System.DateTime))
- return (double)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (double)((TimeSpan)_value).Ticks;
- return (double)ToType(typeof(double), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Double"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent double-precision floating-point number using the
- /// specified culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public double ToDouble(IFormatProvider formatProvider) {
- return (double)ToType(typeof(double), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Single"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent single-precision floating-point number.
- /// </summary>
- public float ToSingle() {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.Single))
- return (float)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return float.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (float)1;
- else
- return (float)0;
- if (tt == typeof(System.DateTime))
- return (float)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (float)((TimeSpan)_value).Ticks;
- return (float)ToType(typeof(float), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Single"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent single-precision floating-point number using the
- /// specified culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public float ToSingle(IFormatProvider formatProvider) {
- return (float)ToType(typeof(float), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Decimal"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent Decimal number.
- /// </summary>
- public decimal ToDecimal() {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.Decimal))
- return (decimal)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return decimal.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (decimal)1;
- else
- return (decimal)0;
- if (tt == typeof(System.DateTime))
- return (decimal)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (decimal)((TimeSpan)_value).Ticks;
- return (decimal)ToType(typeof(decimal), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Decimal"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent Decimal number using the specified culture-specific
- /// formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public decimal ToDecimal(IFormatProvider formatProvider) {
- return (decimal)ToType(typeof(decimal), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.SByte"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 8-bit signed integer.
- /// </summary>
- [CLSCompliant(false)]
- public sbyte ToSByte()
- {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.SByte))
- return (sbyte)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return sbyte.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (SByte)1;
- else
- return (SByte)0;
- if (tt == typeof(System.DateTime))
- return (sbyte)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (sbyte)((TimeSpan)_value).Ticks;
- return (sbyte)ToType(typeof(SByte), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.SByte"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 8-bit signed integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- [CLSCompliant(false)]
- public sbyte ToSByte(IFormatProvider formatProvider)
- {
- return (sbyte)ToType(typeof(sbyte), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.UInt16"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 16-bit unsigned integer.
- /// </summary>
- [CLSCompliant(false)]
- public ushort ToUInt16()
- {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.UInt16))
- return (ushort)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return ushort.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (ushort)1;
- else
- return (ushort)0;
- if (tt == typeof(System.DateTime))
- return (ushort)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (ushort)((TimeSpan)_value).Ticks;
- return (ushort)ToType(typeof(ushort), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.UInt16"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 16-bit unsigned integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- [CLSCompliant(false)]
- public ushort ToUInt16(IFormatProvider formatProvider)
- {
- return (ushort)ToType(typeof(ushort), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.UInt32"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 32-bit unsigned integer.
- /// </summary>
- [CLSCompliant(false)]
- public uint ToUInt32()
- {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.UInt32))
- return (uint)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return uint.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (uint)1;
- else
- return (uint)0;
- if (tt == typeof(System.DateTime))
- return (uint)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (uint)((TimeSpan)_value).Ticks;
- return (uint)ToType(typeof(uint), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.UInt32"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 32-bit unsigned integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- [CLSCompliant(false)]
- public uint ToUInt32(IFormatProvider formatProvider)
- {
- return (uint)ToType(typeof(uint), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.UInt64"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 64-bit unsigned integer.
- /// </summary>
- [CLSCompliant(false)]
- public ulong ToUInt64()
- {
- if (_value == null) return 0;
- Type tt = this.Type;
- if (tt == typeof(System.UInt64))
- return (ulong)_value;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- return ulong.Parse(_value.ToString());
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return (ulong)1;
- else
- return (ulong)0;
- if (tt == typeof(System.DateTime))
- return (ulong)((DateTime)_value).Ticks;
- if (tt == typeof(System.TimeSpan))
- return (ulong)((TimeSpan)_value).Ticks;
- return (ulong)ToType(typeof(ulong), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.UInt64"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent 64-bit unsigned integer using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- [CLSCompliant(false)]
- public ulong ToUInt64(IFormatProvider formatProvider)
- {
- return (ulong)ToType(typeof(ulong), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.DateTime"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent DateTime.
- /// </summary>
- public DateTime ToDateTime() {
- if (_value == null) return DateTime.MinValue;
- Type tt = this.Type;
- if (tt == typeof(System.DateTime))
- return (DateTime)_value;
- if (tt == typeof(System.TimeSpan))
- return new DateTime(((TimeSpan)_value).Ticks);
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- if (this.IsDate)
- return DateTime.Parse(_value.ToString());
- // if (tt == typeof(System.Boolean))
- // throw new InvalidCastException();
- if (this.IsNumberable)
- return new DateTime(this.ToInt64());
- return (DateTime)ToType(typeof(DateTime), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.DateTime"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent DateTime using the specified culture-specific
- /// formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public DateTime ToDateTime(IFormatProvider formatProvider) {
- return (DateTime)ToType(typeof(DateTime), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.TimeSpan"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent TimeSpan.
- /// </summary>
- public TimeSpan ToTimeSpan() {
- if (_value == null) return TimeSpan.MinValue;
- Type tt = this.Type;
- if (tt == typeof(System.TimeSpan))
- return (TimeSpan)_value;
- if (tt == typeof(System.DateTime))
- return new TimeSpan(((DateTime)_value).Ticks);
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- if (this.IsDate)
- return new TimeSpan(DateTime.Parse(_value.ToString()).Ticks);
- // if (tt == typeof(System.Boolean))
- // throw new InvalidCastException();
- if (this.IsNumberable)
- return new TimeSpan(this.ToInt64());
- return (TimeSpan)ToType(typeof(TimeSpan), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.TimeSpan"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent TimeSpan using the specified culture-specific
- /// formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public TimeSpan ToTimeSpan(IFormatProvider formatProvider) {
- return (TimeSpan)ToType(typeof(TimeSpan), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.DateTimeOffset"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent DateTimeOffset.
- /// </summary>
- public DateTimeOffset ToDateTimeOffset() {
- if (_value == null) return DateTimeOffset.MinValue;
- Type tt = this.Type;
- if (tt == typeof(DateTimeOffset))
- return (DateTimeOffset)_value;
- DateTimeOffset result = new DateTimeOffset();
- if (DateTimeOffset.TryParse(_value.ToString(), out result))
- return result;
- return (DateTimeOffset)ToType(typeof(DateTimeOffset), true);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.DateTimeOffset"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance to
- /// an equivalent DateTimeOffset using the specified culture-specific
- /// formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public DateTimeOffset ToDateTimeOffset(IFormatProvider formatProvider) {
- return (DateTimeOffset)ToType(typeof(DateTimeOffset), false, formatProvider);
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Char"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent Unicode character.
- /// </summary>
- public char ToChar() {
- if (_value == null) return (char)0;
- Type tt = this.Type;
- if (tt == typeof(System.Char))
- return (char)_value;
- if (tt == typeof(float) ||
- tt == typeof(double) ||
- tt == typeof(decimal)) {
- return (char)this.ToInt32();
- }
- if (tt == typeof(System.String))
- return char.Parse((string)_value);
- if (tt == typeof(System.Boolean))
- if ((bool)_value)
- return '1';
- else
- return '0';
- try {
- return (char)ToType(typeof(char), true);
- } catch {
- try {
- return char.Parse(_value.ToString());
- } catch {
- try {
- return _value.ToString().ToCharArray()[0];
- } catch {}
- }
- }
- throw new InvalidCastException();
- }
- /// <summary>
- /// If <see cref="Value" /> is a <see cref="System.Char"/>, returns
- /// as-is. Otherwise, attempts to convert the value of this instance
- /// to an equivalent Unicode character using the specified
- /// culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider">The culture-specific formatting information.</param>
- public char ToChar(IFormatProvider formatProvider) {
- return (Char)ToType(typeof(Char), false, formatProvider);
- }
- private enum MathAction {
- Add,
- Subtract,
- Multiply,
- Divide,
- Modulus,
- BitAnd,
- BitOr
- }
- private static object VariantArithmetic(MathAction action, object target, object value) {
- object ret = null;
- Variant sv = new Variant(target);
- Variant av = new Variant(value);
- if (sv.IsNumberable && av.IsNumberable) {
- Double sd = sv.ToDouble();
- Double ad = av.ToDouble();
- long lsd = (long)sd;
- long lad = (long)ad;
- System.Type dt = sv.Type;
- switch (action) {
- case MathAction.Add:
- sd += ad;
- break;
- case MathAction.Subtract:
- sd -= ad;
- break;
- case MathAction.Multiply:
- sd *= ad;
- break;
- case MathAction.Divide:
- sd /= ad;
- break;
- case MathAction.Modulus:
- sd %= ad;
- break;
- case MathAction.BitAnd:
- lsd &= lad;
- sd = (double)lsd;
- break;
- case MathAction.BitOr:
- lsd |= lad;
- sd = (double)lsd;
- break;
- }
- if (sv.IsNumeric) {
- bool isfloat = (sd != Math.Round(sd, 0));
- bool expandminmax = false;
- bool expandfloat = false;
- bool signed = (
- dt == typeof(sbyte) ||
- dt == typeof(short) ||
- dt == typeof(int) ||
- dt == typeof(long) ||
- dt == typeof(float) ||
- dt == typeof(double) ||
- dt == typeof(decimal));
- if ((dt == typeof(byte) && (sd < byte.MinValue || sd > byte.MaxValue)) ||
- (dt == typeof(sbyte) && (sd < sbyte.MinValue || sd > sbyte.MaxValue)) ||
- (dt == typeof(short) && (sd < short.MinValue || sd > short.MaxValue)) ||
- (dt == typeof(ushort) && (sd < ushort.MinValue || sd > ushort.MaxValue)) ||
- (dt == typeof(int) && (sd < int.MinValue || sd > int.MaxValue)) ||
- (dt == typeof(uint) && (sd < uint.MinValue || sd > uint.MaxValue)) ||
- (dt == typeof(long) && (sd < long.MinValue || sd > long.MaxValue)) ||
- (dt == typeof(ulong) && (sd < ulong.MinValue || sd > ulong.MaxValue)) ||
- (dt == typeof(float) && (sd < float.MinValue || sd > float.MaxValue)) ||
- (dt == typeof(decimal) && (sd < (double)decimal.MinValue || sd > (double)decimal.MaxValue)))
- expandminmax = true;
- if (isfloat && (
- dt == typeof(byte) ||
- dt == typeof(sbyte) ||
- dt == typeof(short) ||
- dt == typeof(ushort) ||
- dt == typeof(int) ||
- dt == typeof(uint) ||
- dt == typeof(long) ||
- dt == typeof(ulong)))
- expandfloat = true;
- if (expandfloat) {
- /*if (sd < (double)decimal.MinValue || sd > (double)decimal.MaxValue) {
- ret = sd;
- } else if ((float)sd < (float)float.MinValue || sd > (float)float.MaxValue) {
- ret = (decimal)sd;
- } else {
- ret = (float)sd;
- }*/
- // do we really need all that stuff above?
- // converting to decimal from double makes no sense
- // and converting to float from double causing losing precision
- // so we just assign value
- ret = sd;
- } else if (expandminmax) {
- if (dt == typeof(byte) ||
- dt == typeof(sbyte) ||
- dt == typeof(short) ||
- dt == typeof(ushort) ||
- dt == typeof(int) ||
- dt == typeof(uint) ||
- dt == typeof(long) ||
- dt == typeof(ulong)) {
- if (sd < 0 || signed) {
- long lmin = long.MinValue;
- long lmax = long.MaxValue;
- if (dt == typeof(sbyte)) {
- lmin = sbyte.MinValue;
- lmax = sbyte.MaxValue;
- }
- if (dt == typeof(short)) {
- lmin = short.MinValue;
- lmax = short.MaxValue;
- }
- if (dt == typeof(int)) {
- lmin = int.MinValue;
- lmax = int.MaxValue;
- }
- if (sd < long.MinValue || sd > long.MaxValue ||
- lmin < long.MinValue || lmax > long.MaxValue) {
- ret = sd;
- } else if (sd < int.MinValue || sd > int.MaxValue ||
- lmin < int.MinValue || lmax > int.MaxValue) {
- ret = (long)sd;
- } else if (sd < short.MinValue || sd > short.MaxValue ||
- lmin < short.MinValue || lmax > short.MaxValue) {
- ret = (int)sd;
- } else if (sd < sbyte.MinValue || sd > sbyte.MaxValue ||
- lmin < sbyte.MinValue || lmax > sbyte.MaxValue) {
- ret = (short)sd;
- }
- } else {
- ulong lmax = ulong.MaxValue;
- if (dt == typeof(byte)) lmax = byte.MaxValue;
- if (dt == typeof(ushort)) lmax = ushort.MaxValue;
- if (dt == typeof(uint)) lmax = uint.MaxValue;
- if (sd < ulong.MinValue || sd > ulong.MaxValue ||
- lmax > ulong.MaxValue) {
- ret = sd;
- } else if (sd < uint.MinValue || sd > uint.MaxValue ||
- lmax > uint.MaxValue) {
- ret = (ulong)sd;
- } else if (sd < ushort.MinValue || sd > ushort.MaxValue ||
- lmax > ushort.MaxValue) {
- ret = (uint)sd;
- } else if (sd < byte.MinValue || sd > byte.MaxValue ||
- lmax > byte.MaxValue) {
- ret = (ushort)sd;
- }
- }
- } else {
- ret = sd;
- }
- } else { // Not expandfloat and not expandminmax,
- // so revert to original type!
- ret = System.Convert.ChangeType(sd, sv.Type);
- }
- } else { // not numeric
- Variant v = new Variant(sd);
- ret = v.ToType(sv.Type);
- }
- }
- return ret;
- }
- /// <summary>
- /// Addition operator.
- /// </summary>
- /// <remarks>
- /// If the value on the right is a <see cref="System.String"/>
- /// or a <see cref="System.Char"/>,
- /// the Variant is converted to a string and appended.
- /// If the value on the right or the Variant
- /// is a <see cref="System.DateTime"/>, arithmetic
- /// is performed on the <see cref="DateTime.Ticks"/> property and the
- /// resulting value is set to the DateTime type.
- /// Otherwise, if the value on the right is a number, both
- /// the Variant and the value on the right are
- /// converted to a <see cref="System.Double"/>, the arithmetic
- /// is performed, and the resulting value is converted back to the
- /// original type that the Variant previously represented.
- /// If the type that the Variant previously represented
- /// cannot contain the resulting value--such as if the type is a
- /// <see cref="System.UInt32"/> and the value is <c>-12</c>--then the
- /// type will be converted to a type that can contain
- /// the value, such as <see cref="System.Int32"/>.
- /// </remarks>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>A new <see cref="Variant"/> containing the resulting value.</returns>
- public static Variant operator +(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Type tt = value.GetType();
- if (tt == typeof(bool) && sv.IsNumeric) {
- if ((bool)value) value = 1;
- else value = 0;
- tt = value.GetType();
- }
- if (((sv.Type == typeof(string) || sv.Type == typeof(char)) &&
- !sv.IsNumberable) ||
- (tt == typeof(System.String) || tt == typeof(System.Char)))
- return new Variant(sv.ToString() + value.ToString());
- if ((sv.IsDate && dv.IsDate) ||
- (sv.IsDate && dv.IsTimeSpan) || (sv.IsTimeSpan && dv.IsDate) ||
- (sv.IsDate && dv.IsNumberable)) {
- return new Variant(new DateTime(sv.ToTimeSpan().Ticks + dv.ToTimeSpan().Ticks));
- }
- if (sv.Type == typeof(TimeSpan) && tt == typeof(TimeSpan))
- return new Variant(sv.ToTimeSpan() + dv.ToTimeSpan());
- if (tt == typeof(System.Boolean)) { // change to boolean and toggle if true
- Variant ret = new Variant(sv.ToBoolean());
- if ((bool)value) {
- ret = new Variant(!ret.ToBoolean());
- }
- return ret;
- }
- object retobj = VariantArithmetic(MathAction.Add, sv, value);
- if (retobj != null) return new Variant(retobj);
- throw new InvalidOperationException(
- "Cannot implicitly add value to unidentified type.");
- }
- /// <summary>
- /// Subtraction operator.
- /// </summary>
- /// <remarks>
- /// If the value on the right or the Variant
- /// is a <see cref="System.DateTime"/>, arithmetic
- /// is performed on the <see cref="DateTime.Ticks"/> property and the
- /// resulting value is set to the DateTime type.
- /// Otherwise, if the value on the right is a number, both
- /// the Variant and the value on the right are
- /// converted to a <see cref="System.Double"/>, the arithmetic
- /// is performed, and the resulting value is converted back to the
- /// original type that the Variant previously represented.
- /// If the type that the Variant previously represented
- /// cannot contain the resulting value--such as if the type is a
- /// <see cref="System.UInt32"/> and the value is <c>-12</c>--then the
- /// type will be converted to a type that can contain
- /// the value, such as <see cref="System.Int32"/>.
- /// </remarks>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>A new <see cref="Variant"/> containing the resulting value.</returns>
- public static Variant operator -(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Type tt = value.GetType();
- if (tt == typeof(bool) && sv.IsNumeric) {
- if ((bool)value) value = 1;
- else value = 0;
- tt = value.GetType();
- }
- if ((sv.IsDate && dv.IsDate) ||
- (sv.IsDate && dv.IsTimeSpan) || (sv.IsTimeSpan && dv.IsDate) ||
- (sv.IsDate && dv.IsNumberable)) {
- return new Variant(new DateTime(sv.ToTimeSpan().Ticks - dv.ToTimeSpan().Ticks));
- }
- if (sv.Type == typeof(TimeSpan) && tt == typeof(TimeSpan))
- return new Variant(sv.ToTimeSpan() - dv.ToTimeSpan());
- if (tt == typeof(System.String))
- throw new InvalidOperationException("Objects of type System.ToString() "
- + "cannot subtract.");
- if (tt == typeof(System.Char))
- throw new InvalidOperationException("Objects of type System.ToChar() "
- + "cannot subtract.");
- if (tt == typeof(System.Boolean)) { // change to boolean and toggle if false
- Variant ret = new Variant(sv.ToBoolean());
- if (!(bool)value) {
- ret = new Variant(!ret.ToBoolean());
- }
- return ret;
- }
- object retobj = VariantArithmetic(MathAction.Subtract, sv, value);
- if (retobj != null) return new Variant(retobj);
- throw new InvalidOperationException(
- "Cannot implicitly add value to unidentified type.");
- }
- // AlexTZ
- /// <summary>
- /// Unary minus operator.
- /// </summary>
- public static Variant operator -(Variant subjectVariant)
- {
- return 0 - subjectVariant;
- }
- /// <summary>
- /// Greater than operator.
- /// </summary>
- public static bool operator >(Variant subjectVariant, object value)
- {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant;
- Variant dv = new Variant(value);
- if (sv.IsNumeric && dv.IsNumeric)
- return sv.ToDouble() > dv.ToDouble();
- if (sv.IsDate && dv.IsDate)
- return sv.ToDateTime() > dv.ToDateTime();
- if (sv.IsTimeSpan && dv.IsTimeSpan)
- return sv.ToTimeSpan() > dv.ToTimeSpan();
- return false;
- }
- /// <summary>
- /// Greater than or equal operator.
- /// </summary>
- public static bool operator >=(Variant subjectVariant, object value)
- {
- return subjectVariant > value || subjectVariant == value;
- }
- /// <summary>
- /// Less than operator.
- /// </summary>
- public static bool operator <(Variant subjectVariant, object value)
- {
- return !(subjectVariant > value || subjectVariant == value);
- }
- /// <summary>
- /// Less than or equal operator.
- /// </summary>
- public static bool operator <=(Variant subjectVariant, object value)
- {
- return !(subjectVariant > value);
- }
- /// <summary>
- /// Multiplication operator.
- /// </summary>
- /// <remarks>
- /// If the value on the right or the Variant
- /// is a <see cref="System.DateTime"/>, arithmetic
- /// is performed on the <see cref="DateTime.Ticks"/> property and the
- /// resulting value is set to the DateTime type.
- /// Otherwise, if the value on the right is a number, both
- /// the Variant and the value on the right are
- /// converted to a <see cref="System.Double"/>, the arithmetic
- /// is performed, and the resulting value is converted back to the
- /// original type that the Variant previously represented.
- /// If the type that the Variant previously represented
- /// cannot contain the resulting value--such as if the type is a
- /// <see cref="System.UInt32"/> and the value is <c>-12</c>--then the
- /// type will be converted to a type that can contain
- /// the value, such as <see cref="System.Int32"/>.
- /// </remarks>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>A new <see cref="Variant"/> containing the resulting value.</returns>
- public static Variant operator *(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Type tt = value.GetType();
- if (tt == typeof(bool) && sv.IsNumeric) {
- if ((bool)value) value = 1;
- else value = 0;
- tt = value.GetType();
- }
- if ((sv.IsDate && dv.IsDate) ||
- (sv.IsDate && dv.IsTimeSpan) || (sv.IsTimeSpan && dv.IsDate) ||
- (sv.IsDate && dv.IsNumberable)) {
- return new Variant(new DateTime(sv.ToTimeSpan().Ticks * dv.ToTimeSpan().Ticks));
- }
- if (sv.Type == typeof(TimeSpan) && tt == typeof(TimeSpan))
- return new Variant(new TimeSpan(sv.ToTimeSpan().Ticks * dv.ToTimeSpan().Ticks));
- if (tt == typeof(System.String))
- throw new InvalidOperationException("Objects of type System.ToString() "
- + "cannot multiply.");
- if (tt == typeof(System.Char))
- throw new InvalidOperationException("Objects of type System.ToChar() "
- + "cannot multiply.");
- if (tt == typeof(System.Boolean)) { // change to boolean and multiply by 1 (true) or 0 (false)
- Variant ret = new Variant(sv.ToBoolean());
- if (!(bool)value) {
- ret = new Variant(!ret.ToBoolean());
- }
- return ret;
- }
- object retobj = VariantArithmetic(MathAction.Multiply, sv, value);
- if (retobj != null) return new Variant(retobj);
- throw new InvalidOperationException(
- "Cannot implicitly add value to unidentified type.");
- }
- /// <summary>
- /// Division operator.
- /// </summary>
- /// <remarks>
- /// If the value on the right or the Variant
- /// is a <see cref="System.DateTime"/>, arithmetic
- /// is performed on the <see cref="DateTime.Ticks"/> property and the
- /// resulting value is set to the DateTime type.
- /// Otherwise, if the value on the right is a number, both
- /// the Variant and the value on the right are
- /// converted to a <see cref="System.Double"/>, the arithmetic
- /// is performed, and the resulting value is converted back to the
- /// original type that the Variant previously represented.
- /// If the type that the Variant previously represented
- /// cannot contain the resulting value--such as if the type is a
- /// <see cref="System.UInt32"/> and the value is <c>-12</c>--then the
- /// type will be converted to a type that can contain
- /// the value, such as <see cref="System.Int32"/>.
- /// </remarks>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>A new <see cref="Variant"/> containing the resulting value.</returns>
- public static Variant operator /(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Type tt = value.GetType();
- if (tt == typeof(bool) && sv.IsNumeric) {
- if ((bool)value) value = 1;
- else value = 0;
- tt = value.GetType();
- }
- if ((sv.IsDate && dv.IsDate) ||
- (sv.IsDate && dv.IsTimeSpan) || (sv.IsTimeSpan && dv.IsDate) ||
- (sv.IsDate && dv.IsNumberable)) {
- return new Variant(new DateTime(sv.ToTimeSpan().Ticks / dv.ToTimeSpan().Ticks));
- }
- if (sv.Type == typeof(TimeSpan) && tt == typeof(TimeSpan))
- return new Variant(new TimeSpan(sv.ToTimeSpan().Ticks / dv.ToTimeSpan().Ticks));
- if (tt == typeof(System.String))
- throw new InvalidOperationException("Objects of type System.ToString() "
- + "cannot divide.");
- if (tt == typeof(System.Char))
- throw new InvalidOperationException("Objects of type System.ToChar() "
- + "cannot divide.");
- if (tt == typeof(System.Boolean)) {
- throw new InvalidOperationException("Objects of type System.ToChar() "
- + "cannot apply division.");
- }
- object retobj = VariantArithmetic(MathAction.Divide, sv, value);
- if (retobj != null) return new Variant(retobj);
- throw new InvalidOperationException(
- "Cannot implicitly add value to unidentified type.");
- }
- /// <summary>
- /// Modulus operator.
- /// </summary>
- /// <remarks>
- /// If the value on the right or the Variant
- /// is a <see cref="System.DateTime"/>, arithmetic
- /// is performed on the <see cref="DateTime.Ticks"/> property and the
- /// resulting value is set to the DateTime type.
- /// Otherwise, if the value on the right is a number, both
- /// the Variant and the value on the right are
- /// converted to a <see cref="System.Double"/>, the arithmetic
- /// is performed, and the resulting value is converted back to the
- /// original type that the Variant previously represented.
- /// If the type that the Variant previously represented
- /// cannot contain the resulting value--such as if the type is a
- /// <see cref="System.UInt32"/> and the value is <c>-12</c>--then the
- /// type will be converted to a type that can contain
- /// the value, such as <see cref="System.Int32"/>.
- /// </remarks>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>A new <see cref="Variant"/> containing the resulting value.</returns>
- public static Variant operator %(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Type tt = value.GetType();
- if (tt == typeof(bool) && sv.IsNumeric) {
- if ((bool)value) value = 1;
- else value = 0;
- tt = value.GetType();
- }
- if ((sv.IsDate && dv.IsDate) ||
- (sv.IsDate && dv.IsTimeSpan) || (sv.IsTimeSpan && dv.IsDate) ||
- (sv.IsDate && dv.IsNumberable)) {
- return new Variant(new DateTime(sv.ToTimeSpan().Ticks / dv.ToTimeSpan().Ticks));
- }
- if (sv.Type == typeof(TimeSpan) && tt == typeof(TimeSpan))
- return new Variant(new TimeSpan(sv.ToTimeSpan().Ticks % dv.ToTimeSpan().Ticks));
- if (tt == typeof(System.String))
- throw new InvalidOperationException("Objects of type System.ToString() "
- + "cannot apply modulus.");
- if (tt == typeof(System.Char))
- throw new InvalidOperationException("Objects of type System.ToChar() "
- + "cannot apply modulus.");
- if (tt == typeof(System.Boolean))
- throw new InvalidOperationException("Objects of type System.Boolean "
- + "cannot apply modulus.");
- object retobj = VariantArithmetic(MathAction.Modulus, sv, value);
- if (retobj != null) return new Variant(retobj);
- throw new InvalidOperationException(
- "Cannot implicitly add value to unidentified type.");
- }
- /// <summary>
- /// Bitwise And operator.
- /// </summary>
- /// <remarks>
- /// If the value on the right or the Variant
- /// is a <see cref="System.DateTime"/>, arithmetic
- /// is performed on the <see cref="DateTime.Ticks"/> property and the
- /// resulting value is set to the DateTime type.
- /// Otherwise, if the value on the right is a number, both
- /// the Variant and the value on the right are
- /// converted to a <see cref="System.Double"/>, the arithmetic
- /// is performed, and the resulting value is converted back to the
- /// original type that the Variant previously represented.
- /// If the type that the Variant previously represented
- /// cannot contain the resulting value--such as if the type is a
- /// <see cref="System.UInt32"/> and the value is <c>-12</c>--then the
- /// type will be converted to a type that can contain
- /// the value, such as <see cref="System.Int32"/>.
- /// </remarks>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>A new <see cref="Variant"/> containing the resulting value.</returns>
- public static Variant operator &(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Type tt = value.GetType();
- if (tt == typeof(bool) && sv.IsNumeric) {
- if ((bool)value) value = 1;
- else value = 0;
- tt = value.GetType();
- }
- if (tt == typeof(System.Single))
- throw new InvalidOperationException("Operator '&' cannot "
- + "be applied to operands of type 'float' and 'float'.");
- if (tt == typeof(System.Double))
- throw new InvalidOperationException("Operator '&' cannot "
- + "be applied to operands of type 'double' and 'double'.");
- if (tt == typeof(System.Decimal))
- throw new InvalidOperationException("Operator '&' cannot "
- + "be applied to operands of type 'decimal' and 'decimal'.");
- if ((sv.IsDate && dv.IsDate) ||
- (sv.IsDate && dv.IsTimeSpan) || (sv.IsTimeSpan && dv.IsDate) ||
- (sv.IsDate && dv.IsNumberable)) {
- return new Variant(new DateTime(sv.ToTimeSpan().Ticks & dv.ToTimeSpan().Ticks));
- }
- if (sv.Type == typeof(TimeSpan) && tt == typeof(TimeSpan))
- return new Variant(new TimeSpan(sv.ToTimeSpan().Ticks & dv.ToTimeSpan().Ticks));
- if (tt == typeof(System.String))
- throw new InvalidOperationException("Objects of type System.ToString() "
- + "cannot apply '&' operator.");
- if (tt == typeof(System.Char))
- throw new InvalidOperationException("Objects of type System.ToChar() "
- + "cannot apply '&' operator.");
- if (tt == typeof(System.Boolean))
- return new Variant(sv.ToBoolean() & (bool)value);
- object retobj = VariantArithmetic(MathAction.BitAnd, sv, value);
- if (retobj != null) return new Variant(retobj);
- throw new InvalidOperationException(
- "Cannot implicitly add value to unidentified type.");
- }
- /// <summary>
- /// Bitwise Or operator.
- /// </summary>
- /// <remarks>
- /// If the value on the right or the Variant
- /// is a <see cref="System.DateTime"/>, arithmetic
- /// is performed on the <see cref="DateTime.Ticks"/> property and the
- /// resulting value is set to the DateTime type.
- /// Otherwise, if the value on the right is a number, both
- /// the Variant and the value on the right are
- /// converted to a <see cref="System.Double"/>, the arithmetic
- /// is performed, and the resulting value is converted back to the
- /// original type that the Variant previously represented.
- /// If the type that the Variant previously represented
- /// cannot contain the resulting value--such as if the type is a
- /// <see cref="System.UInt32"/> and the value is <c>-12</c>--then the
- /// type will be converted to a type that can contain
- /// the value, such as <see cref="System.Int32"/>.
- /// </remarks>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>A new <see cref="Variant"/> containing the resulting value.</returns>
- public static Variant operator |(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Type tt = value.GetType();
- if (tt == typeof(bool) && sv.IsNumeric) {
- if ((bool)value) value = 1;
- else value = 0;
- tt = value.GetType();
- }
- if (tt == typeof(System.Single))
- throw new InvalidOperationException("Operator '|' cannot "
- + "be applied to operands of type 'float' and 'float'.");
- if (tt == typeof(System.Double))
- throw new InvalidOperationException("Operator '|' cannot "
- + "be applied to operands of type 'double' and 'double'.");
- if (tt == typeof(System.Decimal))
- throw new InvalidOperationException("Operator '|' cannot "
- + "be applied to operands of type 'decimal' and 'decimal'.");
- if ((sv.IsDate && dv.IsDate) ||
- (sv.IsDate && dv.IsTimeSpan) || (sv.IsTimeSpan && dv.IsDate) ||
- (sv.IsDate && dv.IsNumberable)) {
- return new Variant(new DateTime(sv.ToTimeSpan().Ticks | dv.ToTimeSpan().Ticks));
- }
- if ((sv.Type == typeof(TimeSpan) && tt == typeof(TimeSpan)) ||
- (sv.IsNumberable && tt == typeof(TimeSpan)) ||
- (sv.Type == typeof(TimeSpan) && dv.IsNumberable))
- return new Variant(new TimeSpan(sv.ToTimeSpan().Ticks | dv.ToTimeSpan().Ticks));
- if (tt == typeof(System.String))
- throw new InvalidOperationException("Objects of type System.ToString() "
- + "cannot apply '|' operator.");
- if (tt == typeof(System.Char))
- throw new InvalidOperationException("Objects of type System.ToChar() "
- + "cannot apply '|' operator.");
- if (tt == typeof(System.Boolean))
- return new Variant(sv.ToBoolean() | (bool)value);
- object retobj = VariantArithmetic(MathAction.BitOr, sv, value);
- if (retobj != null) return new Variant(retobj);
- throw new InvalidOperationException(
- "Cannot implicitly add value to unidentified type.");
- }
- /// <summary>
- /// Inequality operator.
- /// </summary>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns>
- /// The opposite of ==
- /// </returns>
- public static bool operator !=(Variant subjectVariant, object value) {
- return !(subjectVariant == value);
- }
- /// <summary>
- /// <para>Equality operator.</para>
- /// <para>First attempts to compare the left value after
- /// temporarily converting it to the type of the right value.
- /// If the conversion cannot occur, such as if the value is not an
- /// intrinsic value type, the comparison occurs at the <see cref="System.Object"/>
- /// level using <b>Object.Equals</b>.</para>
- /// </summary>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool operator ==(Variant subjectVariant, object value) {
- if (value.GetType() == typeof(Variant))
- value = ((Variant)value).Value;
- Variant sv = subjectVariant; // smaller var name :)
- Variant dv = new Variant(value);
- Variant dvv = dv;
- if (sv.IsNumberable && dv.IsNumberable) {
- sv = new Variant(sv.ToDouble());
- dvv = new Variant(dv.ToDouble());
- }
- //if (sv.Type != dvv.Type) return false;
- if (sv.IsDate && dv.IsDate)
- return sv.ToDateTime() == dv.ToDateTime();
- if (sv.IsTimeSpan && dv.IsTimeSpan)
- return sv.ToTimeSpan() == dvv.ToTimeSpan();
- Type tt = dvv.Type;
- if (tt == typeof(System.String) || tt == typeof(System.Char))
- if (sv.ToString() == dvv.ToString()) return true;
- if (dv.IsNumeric)
- return sv.ToDouble() == dv.ToDouble();
- if (dv.Type == typeof(System.Boolean))
- return sv.ToBoolean() == (bool)value;
- return sv.Value == dv.Value ;//Object.Equals(subjectVariant.Value, value);
- }
- /// <summary>
- /// <para>Equality operator.</para>
- /// </summary>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool operator ==(Variant subjectVariant, string value)
- {
- return subjectVariant.ToString() == value;
- }
- /// <summary>
- /// <para>Equality operator.</para>
- /// </summary>
- /// <param name="subjectVariant"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool operator !=(Variant subjectVariant, string value)
- {
- return subjectVariant.ToString() != value;
- }
- /// <summary>
- /// Returns <see cref="String"/> property unless the value on the right
- /// is null. If the value on the right is null, returns "".
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- if (_value == null) return "";
- return this.String;
- }
- /// <summary>
- /// Converts the value of this instance to an equivalent <see cref="String"/>
- /// using the specified culture-specific formatting information.
- /// </summary>
- /// <param name="formatProvider"></param>
- /// <returns></returns>
- public string ToString(IFormatProvider formatProvider) {
- return (string)ToType(typeof(string), false, formatProvider);
- }
- /// <summary>
- /// See <see cref="Object.GetHashCode()"/>.
- /// </summary>
- /// <returns></returns>
- public override int GetHashCode() {
- return base.GetHashCode ();
- }
- /// <summary>
- /// See <see cref="Object.Equals(object)"/>.
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public override bool Equals(object obj) {
- return base.Equals (obj);
- }
- /// <summary>
- /// Converts an object to a boolean.
- /// For any type, if null, returns false.
- /// For Boolean: true/false.
- /// For String: "", "false", "0", etc. == false;
- /// "1", "true", etc. == true, else true.
- /// For numeric intrinsics: 0 == false, else true.
- /// For any other non-null object, returns true.
- /// </summary>
- /// <param name="bln">The string to be converted</param>
- /// <returns>The boolean value of this string.</returns>
- public static bool CBln(object bln) {
- if (bln == null) return false;
- if (bln.GetType() == typeof(bool)) {
- return (bool)bln;
- } else if (bln.GetType() == typeof(string)) {
- string val = (string)bln;
- bool ret = true;
- val = val.ToLower().Trim();
- string sTrue = true.ToString().ToLower();
- string sFalse = false.ToString().ToLower();
- if (val == "" ||
- val == "false" ||
- val == "f" ||
- val == "0" ||
- val == "no" ||
- val == "n" ||
- val == "off" ||
- val == "negative" ||
- val == "neg" ||
- val == "disabled" ||
- val == "incorrect" ||
- val == "wrong" ||
- val == "left" ||
- val == sFalse) {
- ret = false;
- return ret;
- }
- if (val == "true" ||
- val == "t" ||
- val == "1" ||
- val == "-1" ||
- val == "yes" ||
- val == "y" ||
- val == "positive" ||
- val == "pos" ||
- val == "on" ||
- val == "enabled" ||
- val == "correct" ||
- val == "right" ||
- val == sTrue) {
- ret = true;
- return ret;
- }
- try {
- ret = bool.Parse(val);
- } catch {}
- return ret;
- } else if (bln.GetType() == typeof(byte) ||
- bln.GetType() == typeof(ushort) ||
- bln.GetType() == typeof(decimal) ||
- bln.GetType() == typeof(sbyte) ||
- bln.GetType() == typeof(ulong) ||
- bln.GetType() == typeof(int) ||
- bln.GetType() == typeof(uint) ||
- bln.GetType() == typeof(long) ||
- bln.GetType() == typeof(short) ||
- bln.GetType() == typeof(double) ||
- bln.GetType() == typeof(float)) {
- if (bln.ToString() != "0" && bln.ToString() != "0.0") {
- return true;
- } else {
- return false;
- }
- }
- return true;
- }
- public int CompareTo(object other)
- {
- if (this > other) return 1;
- else if (this == other) return 0;
- else return -1;
- }
- // AlexTZ
- ///
- public static implicit operator string(Variant v) {
- return v.ToString();
- }
- ///
- public static implicit operator Variant(string v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator char(Variant v)
- {
- return v.ToChar();
- }
- ///
- public static implicit operator Variant(char v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator byte(Variant v)
- {
- return v.ToByte();
- }
- ///
- public static implicit operator Variant(byte v)
- {
- return new Variant(v);
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator sbyte(Variant v)
- {
- return v.ToSByte();
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator Variant(sbyte v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator short(Variant v)
- {
- return v.ToInt16();
- }
- ///
- public static implicit operator Variant(short v)
- {
- return new Variant(v);
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator ushort(Variant v)
- {
- return v.ToUInt16();
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator Variant(ushort v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator int(Variant v)
- {
- return v.ToInt32();
- }
- ///
- public static implicit operator Variant(int v)
- {
- return new Variant(v);
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator uint(Variant v)
- {
- return v.ToUInt32();
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator Variant(uint v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator long(Variant v)
- {
- return v.ToInt64();
- }
- ///
- public static implicit operator Variant(long v)
- {
- return new Variant(v);
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator ulong(Variant v)
- {
- return v.ToUInt64();
- }
- ///
- [CLSCompliant(false)]
- public static implicit operator Variant(ulong v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator float(Variant v)
- {
- return v.ToSingle();
- }
- ///
- public static implicit operator Variant(float v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator double(Variant v)
- {
- return v.ToDouble();
- }
- ///
- public static implicit operator Variant(double v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator decimal(Variant v)
- {
- return v.ToDecimal();
- }
- ///
- public static implicit operator Variant(decimal v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator bool(Variant v)
- {
- return v.ToBoolean();
- }
- ///
- public static implicit operator Variant(bool v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator DateTime(Variant v)
- {
- return v.ToDateTime();
- }
- ///
- public static implicit operator Variant(DateTime v)
- {
- return new Variant(v);
- }
- ///
- public static implicit operator TimeSpan(Variant v)
- {
- return v.ToTimeSpan();
- }
- ///
- public static implicit operator Variant(TimeSpan v)
- {
- return new Variant(v);
- }
- }
- }
|