本文整理汇总了C#中IDictionary.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IDictionary.GetType方法的具体用法?C# IDictionary.GetType怎么用?C# IDictionary.GetType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDictionary
的用法示例。
在下文中一共展示了IDictionary.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: loadsetting
public loadsetting()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string text = System.IO.File.ReadAllText(settingfile,Encoding.UTF8);
set = new Dictionary<string, object>();
set = (IDictionary<string, object>)serializer.Deserialize(text, set.GetType());
}
示例2: CloneCustomDictionary
private IDictionary<string, object> CloneCustomDictionary(IDictionary<string, object> source)
{
var clone = Activator.CreateInstance(source.GetType()) as IDictionary<string, object>;
if (clone == null) throw new InvalidOperationException("Internal data structure cannot be cloned.");
CopyDictionaryAndCloneNestedDictionaries(source, clone);
return clone;
}
示例3: Convert
public IDictionary Convert(IDictionary source)
{
if (source != null && source.Contains(nameof(Type)))
{
var type = source[nameof(Type)].ToString();
if (Types != null && Types.ContainsKey(type))
{
try
{
var _type = Types[type];
if (_type == null) throw new Exception($"Types['{type}'] was null");
var dictionary = Activator.CreateInstance(_type) as IDictionary;
if(dictionary == null)
{
throw new Exception($"unable to create instance of type {_type.FullName}");
}
Copy(source, dictionary);
return dictionary;
}
catch(Exception ex)
{
throw new Exception($"Exception while converting type '{type}', fullname {Types[type].FullName}", ex);
}
}
}
if(source != null)
{
var result = Activator.CreateInstance(source.GetType()) as IDictionary;
Copy(source, result);
return result;
}
return source;
}
示例4: IsValidDictionaryType
private static bool IsValidDictionaryType(IDictionary dictionary)
{
if (dictionary == null)
{
return false;
}
Type[] genericParameters = dictionary.GetType().GetGenericArguments();
// Support non-generics IDictionary
if (genericParameters.Length == 0)
{
return true;
}
// Only support IDictionary<string|object, object>
if (genericParameters[0] != typeof(string) && genericParameters[0] != typeof(object))
{
return false;
}
if (genericParameters[1] != typeof(object))
{
return false;
}
return true;
}
示例5: GetClientClass
protected string GetClientClass( IDictionary dictionary )
{
Type type = dictionary.GetType();
string className = type.IsGenericType && type.FullName != null
? type.FullName.Substring( 0, type.FullName.IndexOf( "`" ) )
: type.FullName;
#if (FULL_BUILD)
string clientClass = null;
string mappingClassName = ORBConstants.CLIENT_MAPPING + className;
IDictionary props = ThreadContext.getProperties();
if( props.Contains( className ) )
clientClass = (string) props[ mappingClassName ];
#else
string clientClass = null;
#endif
if( clientClass == null )
{
clientClass = Types.Types.getClientClassForServerType( className );
if( clientClass == null && type.IsGenericType )
{
Type keyType = type.GetGenericArguments()[ 0 ];
if( !keyType.IsPrimitive && !StringUtil.IsStringType( keyType ) )
clientClass = "flash.utils.Dictionary";
}
return clientClass;
}
else
{
return clientClass;
}
}
示例6: CreateFunc
public static Func<int, IDictionary<string, object>> CreateFunc(IDictionary<string, object> source)
{
var dictionary = source as Dictionary<string, object>;
if (dictionary != null) return cap => new Dictionary<string, object>(cap, dictionary.Comparer);
var sortedDictionary = source as SortedDictionary<string, object>;
if (sortedDictionary != null) return cap => new SortedDictionary<string, object>(sortedDictionary.Comparer);
if (source is ConcurrentDictionary<string,object>) return cap => new ConcurrentDictionary<string, object>();
var type = source.GetType();
return cap => (IDictionary<string, object>) Activator.CreateInstance(type);
}
示例7: ConvertTypes
public static IDictionary ConvertTypes(IDictionary source, Dictionary<string, Type> types,string typeKey = "Type")
{
if (source == null) return null;
if (types == null) return source;
var copy = Activator.CreateInstance(source.GetType()) as IDictionary;
if (copy == null) throw new Exception($"failed to create instance of type {source.GetType().FullName}");
var typename = GetTypeName(source,typeKey);
if (typename.Length > 0 && types.ContainsKey(typename))
{
var targetType = types[typename];
if (targetType == null) throw new Exception($"types['{typename}'] was null");
if (source.GetType() != targetType)
{
copy = Activator.CreateInstance(targetType) as IDictionary;
if (copy == null) throw new Exception($"failed to create instance of type {targetType.FullName}");
}
}
foreach (var key in source.Keys)
{
var value = source[key];
var childDictionary = value as IDictionary;
if (childDictionary != null)
{
copy[key] = ConvertTypes(childDictionary, types,typeKey);
}
else
{
var childEnumerable = value as IEnumerable;
if (childEnumerable != null && childEnumerable.GetType() != typeof(string))
{
copy[key] = IEnumerableExtension.ConvertTypes(childEnumerable, types,typeKey);
}
else
{
if (copy.Contains(key)) copy[key] = value;
else copy.Add(key, value);
}
}
}
return copy;
}
示例8: BenchImpl
private void BenchImpl(IDictionary<int, string> dictionary, int count)
{
dictionary[0] = "0"; // Force JIT
var watch = Stopwatch.StartNew();
for (int i = 0; i < count; i++)
dictionary[i] = i.ToString();
string ignored;
for (int i = 0; i < count; i++)
ignored = dictionary[i];
_output.WriteLine(string.Format("Time to process with dictionary of type '{0}': {1}.",
dictionary.GetType(),
watch.Elapsed));
}
示例9: Create
private IDictionary Create(IDictionary source)
{
IDictionary result = null;
if (source.Contains(nameof(Type)))
{
var type = source[nameof(Type)].ToString();
if (Types != null && Types.ContainsKey(type))
{
result = Activator.CreateInstance(Types[type]) as IDictionary;
}
}
if(result == null) result = System.Activator.CreateInstance(source.GetType()) as IDictionary;
return result;
}
示例10: BenchImpl
private static void BenchImpl(IDictionary<string, int> dictionary, int count)
{
Console.WriteLine("Start...");
var watch = Stopwatch.StartNew();
for (int i = 0; i < count; i++)
dictionary[i.ToString()] = i;
var sets = watch.Elapsed;
Console.WriteLine(string.Format(" Sets: {0}.", sets));
int ignored;
for (int i = 0; i < count; i++)
ignored = dictionary[i.ToString()];
Console.WriteLine(string.Format(" Gets: {0}.", watch.Elapsed - sets));
Console.WriteLine(string.Format("Global time to process with dictionary of type '{0}': {1}.",
dictionary.GetType(),
watch.Elapsed));
}
示例11: AddKeyValueTest
public void AddKeyValueTest(IDictionary<int, object> anyDictionary, int size)
{
const string path = @"E:\results.txt";
StreamWriter tr = File.AppendText(path);
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < size; i++)
{
anyDictionary.Add(i, "Value" + i);
}
sw.Stop();
tr.WriteLine("AddKeyValueTest for " + anyDictionary.GetType().Name + " size: " + size + " took {0}ms",
sw.ElapsedMilliseconds.ToString());
Console.WriteLine("Elapsed time for AddKeyValue is: {0}ms", sw.Elapsed);
Assert.AreEqual(size, anyDictionary.Count);
sw.Reset();
tr.Close();
tr.Dispose();
}
示例12: CloneDictionary
internal static IDictionary CloneDictionary(IDictionary source)
{
if (source == null)
{
return null;
}
if (source is ICloneable)
{
return (IDictionary) ((ICloneable) source).Clone();
}
IDictionary dictionary = (IDictionary) Activator.CreateInstance(source.GetType());
IDictionaryEnumerator enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
{
ICloneable key = enumerator.Key as ICloneable;
ICloneable cloneable2 = enumerator.Value as ICloneable;
if ((key != null) && (cloneable2 != null))
{
dictionary.Add(key.Clone(), cloneable2.Clone());
}
}
return dictionary;
}
示例13: TypedMapType
public TypedMapType(IDictionary value)
{
Type[] argumentTypes = value.GetType().GetInterface("IDictionary`2").GetGenericArguments();
keyType = JmxTypeMapping.GetJmxXmlType(argumentTypes[0].AssemblyQualifiedName);
valueType = JmxTypeMapping.GetJmxXmlType(argumentTypes[1].AssemblyQualifiedName);
List<MapTypeEntry> mapTypeEntries = new List<MapTypeEntry>();
foreach (DictionaryEntry entry in value)
{
mapTypeEntries.Add(new MapTypeEntry
{
Key = new GenericValueType(entry.Key),
Value = new GenericValueType(entry.Value)
});
}
Entry = mapTypeEntries.ToArray();
}
示例14: SerializeDictionary
private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract)
{
contract.InvokeOnSerializing(values);
SerializeStack.Add(values);
writer.WriteStartObject();
bool isReference = contract.IsReference ?? HasFlag(_serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(_serializer.ReferenceResolver.GetReference(values));
}
if (HasFlag(_serializer.TypeNameHandling, TypeNameHandling.Objects))
{
WriteTypeProperty(writer, values.GetType());
}
foreach (DictionaryEntry entry in values)
{
string propertyName = entry.Key.ToString();
object value = entry.Value;
if (ShouldWriteReference(value, null))
{
writer.WritePropertyName(propertyName);
WriteReference(writer, value);
}
else
{
if (!CheckForCircularReference(value, null))
continue;
writer.WritePropertyName(propertyName);
SerializeValue(writer, value, null);
}
}
writer.WriteEndObject();
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(values);
}
示例15: RemoveElementByKeyTest
public void RemoveElementByKeyTest(IDictionary<int, object> anyDictionary, int size)
{
const string path = @"E:\results.txt";
StreamWriter tr = File.AppendText(path);
//Lets add 3 items in the dictionary
for (int i = 0; i < size; i++)
{
anyDictionary.Add(i, "Value" + i);
}
Assert.AreEqual(size, anyDictionary.Count);
var s = new Stopwatch();
s.Start();
for (int i = 0; i < size; i++)
{
anyDictionary.Remove(i);
}
//anyDictionary.Remove(99);
s.Stop();
Console.WriteLine("Elapsed time for RemoveKey is: {0}ms", s.Elapsed);
tr.WriteLine("RemoveByKey for " + anyDictionary.GetType().Name + " size: " + size + " took {0}ms",
s.ElapsedMilliseconds);
Assert.AreEqual(0, anyDictionary.Count);
s.Reset();
tr.Close();
tr.Dispose();
}