本文整理汇总了C#中System.Variant.As方法的典型用法代码示例。如果您正苦于以下问题:C# Variant.As方法的具体用法?C# Variant.As怎么用?C# Variant.As使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Variant
的用法示例。
在下文中一共展示了Variant.As方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestDateTime
public void TestDateTime()
{
Variant v1 = new Variant(DateTime.MaxValue);
byte[] bytes1 = BinaryWriter.ToBytes(v1);
Variant v2 = BinaryReader.FromBytes(bytes1);
Assert.AreEqual(v1.As<DateTime>(), v2.As<DateTime>());
Variant v3 = new Variant(DateTime.MinValue);
byte[] bytes2 = BinaryWriter.ToBytes(v3);
Variant v4 = BinaryReader.FromBytes(bytes2);
Assert.AreEqual(v3.As<DateTime>(), v4.As<DateTime>());
}
示例2: TestDateTime
public void TestDateTime()
{
DateTime arg = new DateTime(2010, 1, 2, 3, 4, 5, 6);
Variant v1 = new Variant(arg);
Assert.AreEqual(v1.Type, Variant.EnumType.DateTime);
Assert.IsTrue(v1.Is(Variant.EnumType.DateTime));
Assert.IsTrue(v1.Is<DateTime>());
Assert.AreEqual(v1.As<DateTime>(), arg);
Assert.AreEqual(Convert.ToDateTime(v1), arg);
Assert.AreEqual(v1.ToString(), "2010-01-02T03:04:05.006");
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
}
示例3: TestAny
public void TestAny()
{
Variant v1 = new Variant(Variant.EnumType.Any, "1.0");
Assert.AreEqual(v1.Type, Variant.EnumType.Any);
Assert.IsTrue(v1.Is(Variant.EnumType.Any));
Assert.AreEqual(1.0, v1.As<double>());
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
// Enumerating an Any variant with content should throw an exception
Assert.Throws<VariantException>(delegate { v1.GetEnumerator(); });
// We should be able to enumerate an Any variant with no content as this is a common occurance when reading untyped XML
Variant v3 = new Variant(Variant.EnumType.Any);
IEnumerator<VariantItem> enumerator = v3.GetEnumerator();
Assert.IsFalse(enumerator.MoveNext());
Assert.Throws<VariantException>(delegate { VariantItem item = enumerator.Current; });
}
示例4: TestBoolean
public void TestBoolean()
{
Variant vTrue = new Variant(true);
Assert.AreEqual(vTrue.Type, Variant.EnumType.Boolean);
Assert.IsTrue(vTrue.Is(Variant.EnumType.Boolean));
Assert.IsTrue(vTrue.Is<bool>());
Assert.IsTrue(vTrue.As<bool>());
Assert.IsTrue(Convert.ToBoolean(vTrue));
Assert.AreEqual(vTrue.ToString(), "true");
Variant vFalse = new Variant(false);
Assert.AreEqual(vFalse.Type, Variant.EnumType.Boolean);
Assert.IsTrue(vFalse.Is(Variant.EnumType.Boolean));
Assert.IsTrue(vFalse.Is<bool>());
Assert.IsFalse(vFalse.As<bool>());
Assert.IsFalse(Convert.ToBoolean(vFalse));
Assert.AreEqual(vFalse.ToString(), "false");
Variant v1 = new Variant(vTrue);
Assert.IsTrue(v1.Equals(vTrue));
Variant v2 = new Variant(vFalse);
Assert.IsTrue(v2.Equals(vFalse));
}
示例5: TestConstruction
public void TestConstruction()
{
System.Data.DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("Double", typeof(double)));
dt1.Columns.Add(new DataColumn("String", typeof(string)));
DataRow dr = dt1.NewRow();
dr[0] = 1.0;
dr[1] = "value";
dt1.Rows.Add(dr);
Variant v1 = new Variant(dt1);
Assert.AreEqual(v1.Type, Variant.EnumType.DataTable);
Assert.IsTrue(v1.Is(Variant.EnumType.DataTable));
Assert.DoesNotThrow(delegate { v1.AsDataTable(); });
Assert.Throws<VariantException>(delegate { v1.As<int>(); });
}
示例6: WriteText
void WriteText(Variant text)
{
switch(text.Type)
{
case VariantBase.EnumType.Any:
case VariantBase.EnumType.String:
{
m_writer.WriteString(text.As<string>());
break;
}
case VariantBase.EnumType.Int32:
case VariantBase.EnumType.UInt32:
case VariantBase.EnumType.Int64:
case VariantBase.EnumType.UInt64:
case VariantBase.EnumType.Float:
case VariantBase.EnumType.Double:
case VariantBase.EnumType.Boolean:
case VariantBase.EnumType.Date:
case VariantBase.EnumType.Time:
case VariantBase.EnumType.DateTime:
{
m_writer.WriteString(text.AnyCast().As<string>());
break;
}
default:
throw new VariantException("Attempt to output non-primitive variant as text: " + text.Type);
}
}
示例7: WriteComment
void WriteComment(Variant comment)
{
m_writer.WriteComment(comment.As<string>());
}
示例8: TestDouble
public void TestDouble()
{
Variant v1 = new Variant((double)5.0);
Assert.AreEqual(v1.Type, Variant.EnumType.Double);
Assert.IsTrue(v1.Is(Variant.EnumType.Double));
Assert.IsTrue(v1.Is<double>());
Assert.AreEqual(v1.As<double>(), 5.0);
Assert.AreEqual(Convert.ToDouble(v1), 5.0);
Assert.AreEqual(v1.ToString(), "5");
Variant vNaN = new Variant(double.NaN);
Assert.AreEqual(vNaN.ToString(), "NaN");
Variant vINF = new Variant(double.PositiveInfinity);
Assert.AreEqual(vINF.ToString(), "INF");
Variant vNEGINF = new Variant(double.NegativeInfinity);
Assert.AreEqual(vNEGINF.ToString(), "-INF");
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
}
示例9: TestUInt64
public void TestUInt64()
{
Variant v1 = new Variant((UInt64)4);
Assert.AreEqual(v1.Type, Variant.EnumType.UInt64);
Assert.IsTrue(v1.Is(Variant.EnumType.UInt64));
Assert.IsTrue(v1.Is<UInt64>());
Assert.AreEqual(v1.As<UInt64>(), 4);
Assert.AreEqual(Convert.ToUInt64(v1), 4);
Assert.AreEqual(v1.ToString(), "4");
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
}
示例10: TestTime
public void TestTime()
{
TimeSpan arg = new TimeSpan(0, 2, 3, 4, 5);
Variant v1 = new Variant(arg);
Assert.AreEqual(v1.Type, Variant.EnumType.Time);
Assert.IsTrue(v1.Is(Variant.EnumType.Time));
Assert.IsTrue(v1.Is<TimeSpan>());
Assert.AreEqual(v1.As<TimeSpan>(), arg);
Assert.AreEqual(v1.ToString(), "02:03:04.005");
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
}
示例11: TestString
public void TestString()
{
Variant v1 = new Variant("test string");
Assert.AreEqual(v1.Type, Variant.EnumType.String);
Assert.IsTrue(v1.Is(Variant.EnumType.String));
Assert.IsTrue(v1.Is<string>());
Assert.AreEqual(v1.As<string>(), "test string");
Assert.AreEqual(Convert.ToString(v1), "test string");
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
}
示例12: TestParsing
public void TestParsing()
{
// Time
Variant v1 = new Variant(Variant.EnumType.Any, "02:03:04.005");
Assert.AreEqual(new TimeSpan(0, 2, 3, 4, 5), v1.As<TimeSpan>());
Variant v2 = new Variant(Variant.EnumType.Any, "02:03:04");
Assert.AreEqual(new TimeSpan(2, 3, 4), v2.As<TimeSpan>());
// DateTime
Variant v3 = new Variant(Variant.EnumType.Any, "2010-01-02T03:04:05.006");
Assert.AreEqual(new DateTime(2010, 1, 2, 3, 4, 5, 6), v3.As<DateTime>());
Variant v4 = new Variant(Variant.EnumType.Any, "2010-01-02T03:04:05");
Assert.AreEqual(new DateTime(2010, 1, 2, 3, 4, 5, 0), v4.As<DateTime>());
// Double
Variant v5 = new Variant(Variant.EnumType.Any, "NaN");
Assert.IsTrue(double.IsNaN(v5.As<Double>()));
Variant v6 = new Variant(Variant.EnumType.Any, "INF");
Assert.IsTrue(double.IsPositiveInfinity(v6.As<Double>()));
Variant v7 = new Variant(Variant.EnumType.Any, "-INF");
Assert.IsTrue(double.IsNegativeInfinity(v7.As<Double>()));
// String
Variant v8 = new Variant(Variant.EnumType.Any, "value");
Assert.AreEqual("value", v8.As<string>());
}
示例13: TestInt64
public void TestInt64()
{
Variant v1 = new Variant((Int64)3);
Assert.AreEqual(v1.Type, Variant.EnumType.Int64);
Assert.IsTrue(v1.Is(Variant.EnumType.Int64));
Assert.IsTrue(v1.Is<Int64>());
Assert.AreEqual(v1.As<Int64>(), 3);
Assert.AreEqual(Convert.ToInt64(v1), 3);
Assert.AreEqual(v1.ToString(), "3");
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
}
示例14: TestInt32
public void TestInt32()
{
Variant v1 = new Variant((Int32)1);
Assert.AreEqual(v1.Type, Variant.EnumType.Int32);
Assert.IsTrue(v1.Is(Variant.EnumType.Int32));
Assert.IsTrue(v1.Is<Int32>());
Assert.AreEqual(v1.As<Int32>(), 1);
Assert.AreEqual(Convert.ToInt32(v1), 1);
Assert.AreEqual(v1.ToString(), "1");
Variant v2 = new Variant(v1);
Assert.IsTrue(v1.Equals(v2));
}
示例15: WriteVariant
protected void WriteVariant(Variant v)
{
VariantBase.EnumType type = v.Type;
Write((Int32)type);
switch (type)
{
case VariantBase.EnumType.None:
break;
case VariantBase.EnumType.String:
case VariantBase.EnumType.Any:
Write(v.As<string>());
break;
case VariantBase.EnumType.Float:
Write(v.As<float>());
break;
case VariantBase.EnumType.Double:
Write(v.As<double>());
break;
case VariantBase.EnumType.Int32:
Write(v.As<Int32>());
break;
case VariantBase.EnumType.UInt32:
Write(v.As<UInt32>());
break;
case VariantBase.EnumType.Int64:
Write(v.As<Int64>());
break;
case VariantBase.EnumType.UInt64:
Write(v.As<UInt64>());
break;
case VariantBase.EnumType.Boolean:
Write(v.As<bool>());
break;
case VariantBase.EnumType.Time:
Write(v.As<TimeSpan>());
break;
case VariantBase.EnumType.DateTime:
Write(v.As<DateTime>());
break;
case VariantBase.EnumType.List:
case VariantBase.EnumType.Tuple:
Write(v.Count);
foreach (VariantItem item in v)
{
WriteVariant(item.Value);
}
break;
case VariantBase.EnumType.Dictionary:
case VariantBase.EnumType.Bag:
Write(v.Count);
foreach (VariantItem item in v)
{
Write(item.Key);
WriteVariant(item.Value);
}
break;
case VariantBase.EnumType.TimeSeries:
Write(v.Count);
foreach (VariantItem item in v)
{
Write(item.Time);
WriteVariant(item.Value);
}
break;
case VariantBase.EnumType.Object:
IVariantObject o = v.AsObject();
Write(o.Class);
Write(o.Version);
WriteVariant(o.Deflate());
break;
case VariantBase.EnumType.Exception:
VariantExceptionInfo x = v.AsException();
Write(x.Class);
Write(x.Message);
Write(x.Source);
Write(x.Stack);
break;
case VariantBase.EnumType.Buffer:
Write(v.AsBuffer().Length);
Write(v.AsBuffer(), true);
break;
case VariantBase.EnumType.DataTable:
Write(v.AsDataTable());
break;
case VariantBase.EnumType.Array:
Write(v.AsArray());
break;
default:
throw new VariantException("Case exhaustion: " + type.ToString());
}
}