本文整理汇总了C#中Dictionary.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.GetType方法的具体用法?C# Dictionary.GetType怎么用?C# Dictionary.GetType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dictionary
的用法示例。
在下文中一共展示了Dictionary.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
Type guyType = typeof(Guy);
Console.WriteLine("{0} extends {1}",
guyType.FullName,
guyType.BaseType.FullName);
// output: TypeExamples.Guy extends System.Object
Type nestedClassType = typeof(NestedClass.DoubleNestedClass);
Console.WriteLine(nestedClassType.FullName);
// output: TypeExamples.Program+NestedClass+DoubleNestedClass
List<Guy> guyList = new List<Guy>();
Console.WriteLine(guyList.GetType().Name);
// output: List`1
Dictionary<string, Guy> guyDictionary = new Dictionary<string, Guy>();
Console.WriteLine(guyDictionary.GetType().Name);
// output: Dictionary`2
Type t = typeof(Program);
Console.WriteLine(t.FullName);
// output: TypeExamples.Program
Type intType = typeof(int);
Type int32Type = typeof(Int32);
Console.WriteLine("{0} - {1}", intType.FullName, int32Type.FullName);
// System.Int32 - System.Int32
Console.WriteLine("{0} {1}", float.MinValue, float.MaxValue);
// output:-3.402823E+38 3.402823E+38
Console.WriteLine("{0} {1}", int.MinValue, int.MaxValue);
// output:-2147483648 2147483647
Console.WriteLine("{0} {1}", DateTime.MinValue, DateTime.MaxValue);
// output: 1/1/0001 12:00:00 AM 12/31/9999 11:59:59 PM
Console.WriteLine(12345.GetType().FullName);
// output: System.Int32
Console.ReadKey();
}
示例2: Write_BuiltsXmlNode_IfAGenericDictionary
public void Write_BuiltsXmlNode_IfAGenericDictionary()
{
var parentNode = NewMock<INodeWriter>();
var dictNode = CreateXmlNode("topNode");
var itemsNode = CreateXmlNode("dictionary");
var value = new Dictionary<string, int>();
value.Add("first key", 11);
StubTypeMapper();
Expect.Once.On(valuesCache).Method("Add").With(value).Will(Return.Value(7));
Expect.Once.On(ownerDocument).Method("CreateTypedElement").With("dict", value.GetType().ToString(),
parentNode).Will(
Return.Value(dictNode));
Expect.Once.On(dictNode).Method("AddAttribute").With("ID", "7");
Expect.Once.On(dictNode).Method("Dispose").WithNoArguments();
Expect.Once.On(itemsNode).Method("Dispose").WithNoArguments();
Expect.Once.On(ownerDocument).Method("CreateElement").With("items", dictNode).Will(Return.Value(itemsNode));
Expect.Once.On(objectWriter).Method("Write").With("first key", itemsNode, typeof (string));
Expect.Once.On(objectWriter).Method("Write").With(11, itemsNode, typeof (Int32));
Expect.Once.On(itemsNode).Method("AddAttribute").With("count", 1);
writer.Write(value, parentNode, value.GetType());
}
示例3: RegisterClick
private void RegisterClick(object sender, RoutedEventArgs e)
{
if (UsernameField.Text == "" || PasswordField.Password == "" || ConfirmPasswordField.Password == "")
{
ErrorField.Text = "All fields are mandatory.";
return;
}
if (PasswordField.Password != ConfirmPasswordField.Password)
{
ErrorField.Text = "Password and Confirmation don't match.";
return;
}
ErrorField.Text = "";
APIRequest request = new APIRequest(APIRequest.POST, this, APIRequest.requestCodeType.Register, "register");
Dictionary<string, string> dict = new Dictionary<string, string>()
{
{"username", UsernameField.Text},
{"password", PasswordField.Password}
};
var serializer = new DataContractJsonSerializer(dict.GetType(), new DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat = true
});
MemoryStream stream = new MemoryStream();
serializer.WriteObject(stream, dict);
byte[] bytes = stream.ToArray();
string content = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
request.Execute(null, content);
}
示例4: GetDictionaryType_StringInt
public void GetDictionaryType_StringInt()
{
var dsi = new Dictionary<string, int>();
Type keyType, valueType;
ReflectionHelper.GetDictionaryType(dsi.GetType(), out keyType, out valueType);
Assert.AreEqual(typeof(string), keyType);
Assert.AreEqual(typeof(int), valueType);
}
示例5: GetDictionaryType_DoubleDateTime
public void GetDictionaryType_DoubleDateTime()
{
var dsi = new Dictionary<double, DateTime>();
Type keyType, valueType;
ReflectionHelper.GetDictionaryType(dsi.GetType(), out keyType, out valueType);
Assert.AreEqual(typeof(double), keyType);
Assert.AreEqual(typeof(DateTime), valueType);
}
示例6: checkifgenericdictionary
public void checkifgenericdictionary()
{
var dict = new Dictionary<string, object>();
//Debug.WriteLine(IsGenericDictionary(dict.GetType(), dict));
Type contextType = dict.GetType();
if (contextType.GetInterface("IDictionary`2") != null)
{
}
}
示例7: GetGenericArgumentCount_WhenGivenNonGenericArguments_ReturnsTwo
public void GetGenericArgumentCount_WhenGivenNonGenericArguments_ReturnsTwo()
{
// ARRANGE
var item = new Dictionary<int, string>();
var type = item.GetType();
// ACT
var actual = type.GetGeneGetGenricArgumentCount();
// ASSERT
Assert.AreEqual(2, actual);
}
示例8: WriteTypeRegularTest
public void WriteTypeRegularTest()
{
Dictionary<string, IEnumerable<DateTime>[]> o = new Dictionary<string, IEnumerable<DateTime>[]>();
Type t = o.GetType();
TypeWriter writer = new TypeWriter();
Console.WriteLine(writer.WriteType(t));
Console.WriteLine(writer.WriteType(t.GetGenericTypeDefinition()));
Assert.AreEqual("System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Collections.Generic.IEnumerable`1[[System.DateTime, mscorlib]][], mscorlib]], mscorlib", writer.WriteType(t));
Assert.AreEqual("System.Collections.Generic.Dictionary`2, mscorlib", writer.WriteType(t.GetGenericTypeDefinition()));
}
示例9: WriteTypeMinimalTest
public void WriteTypeMinimalTest()
{
Dictionary<string, IEnumerable<DateTime>[]> o = new Dictionary<string, IEnumerable<DateTime>[]>();
Type t = o.GetType();
TypeWriter writer = new TypeWriter();
writer.WithAssembly = false;
Console.WriteLine(writer.WriteType(t));
Console.WriteLine(writer.WriteType(t.GetGenericTypeDefinition()));
Assert.AreEqual("System.Collections.Generic.Dictionary`2[[System.String],[System.Collections.Generic.IEnumerable`1[[System.DateTime]][]]]", writer.WriteType(t));
Assert.AreEqual("System.Collections.Generic.Dictionary`2", writer.WriteType(t.GetGenericTypeDefinition()));
}
示例10: GetAllHints
public static string GetAllHints(string etid)
{
List<DJ_Workers> ListGw= new BLLDJTourGroup().GetTourGroupByTeId(int.Parse(etid)).ToList();
Dictionary<string, string> data = new Dictionary<string, string>();
foreach (DJ_Workers item in ListGw)
{
data.Add(Guid.NewGuid().ToString(), item.Name + "/" + item.IDCard.Substring(0, 6) + "********" + item.IDCard.Substring(14));
}
DataContractJsonSerializer serializer = new DataContractJsonSerializer(data.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, data);
return System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
}
示例11: WriteAndReadTypeTest
public void WriteAndReadTypeTest()
{
Dictionary<string, IEnumerable<DateTime>[]> o = new Dictionary<string, IEnumerable<DateTime>[]>();
Type t = o.GetType();
TypeWriter writer = new TypeWriter();
Console.WriteLine(writer.WriteType(t));
Console.WriteLine(writer.WriteType(t.GetGenericTypeDefinition()));
Type t1 = Type.GetType(writer.WriteType(t));
Type t2 = Type.GetType(writer.WriteType(t.GetGenericTypeDefinition()));
Assert.AreEqual(t, t1);
Assert.AreEqual(t.GetGenericTypeDefinition(), t2);
}
示例12: WriteTypeFullTest
public void WriteTypeFullTest()
{
Dictionary<string, IEnumerable<DateTime>[]> o = new Dictionary<string, IEnumerable<DateTime>[]>();
Type t = o.GetType();
TypeWriter writer = new TypeWriter();
writer.WithVersion = true;
writer.WithCulture = true;
writer.WithPublicKeyToken = true;
Console.WriteLine(writer.WriteType(t));
Console.WriteLine(writer.WriteType(t.GetGenericTypeDefinition()));
Assert.AreEqual("System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.IEnumerable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", writer.WriteType(t));
Assert.AreEqual("System.Collections.Generic.Dictionary`2, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", writer.WriteType(t.GetGenericTypeDefinition()));
}
示例13: TestDataBindingDictionary
public void TestDataBindingDictionary()
{
var dict = new Dictionary<string, double>
{
{"zero", 3},
{"one", 3.1},
{"two", 3.14}
};
var collectionInfo = CollectionInfo.ForType(dict.GetType());
CollectionAssert.AreEqual(dict.Keys, collectionInfo.GetKeys(dict).Cast<string>().ToArray());
CollectionAssert.AreEqual(dict, collectionInfo.GetItems(dict).Cast<KeyValuePair<string,double>>().ToArray());
foreach (var key in collectionInfo.GetKeys(dict))
{
var kvp = (KeyValuePair<string, double>) collectionInfo.GetItemFromKey(dict, key);
Assert.AreEqual(key, kvp.Key);
Assert.AreEqual(dict[(string)key], kvp.Value);
}
}
示例14: Execute
public void Execute()
{
var theList = new List<int> {1, 2, 3, 4, 5};
var theDictionary = new Dictionary<int, string> {{1, "hoge"}, {2, "hehe"}};
//
// Genericなオブジェクトの型引数の型を取得するには、System.Typeクラスの以下のメソッドを利用する。
//
// ・GetGenericArguments()
//
// GetGenericArgumentsメソッドは、System.Typeの配列を返すので、これを利用して型引数の型を判別する。
//
var genericArgTypes = theList.GetType().GetGenericArguments();
Output.WriteLine("=============== List<int>の場合 =================");
Output.WriteLine("型引数の数={0}, 型引数の型=({1})", genericArgTypes.Count(), string.Join(",", genericArgTypes.Select(item => item.Name)));
genericArgTypes = theDictionary.GetType().GetGenericArguments();
Output.WriteLine("=============== Dictionary<int, string>の場合 =================");
Output.WriteLine("型引数の数={0}, 型引数の型=({1})", genericArgTypes.Count(), string.Join(",", genericArgTypes.Select(item => item.Name)));
}
示例15: GetAllHints
public static string GetAllHints()
{
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("苹果4代iphone正品", "21782");
data.Add("苹果4代 手机套", "238061");
data.Add("苹果4", "838360");
data.Add("苹果皮", "242721");
data.Add("苹果笔记本", "63348");
data.Add("苹果4s", "24030");
data.Add("戴尔笔记本", "110105");
data.Add("戴尔手机", "18870");
data.Add("戴尔键盘", "30367");
DataContractJsonSerializer serializer = new DataContractJsonSerializer(data.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, data);
return System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
}