本文整理汇总了C#中Cache.GetTypeCache方法的典型用法代码示例。如果您正苦于以下问题:C# Cache.GetTypeCache方法的具体用法?C# Cache.GetTypeCache怎么用?C# Cache.GetTypeCache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cache
的用法示例。
在下文中一共展示了Cache.GetTypeCache方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Fetch
internal override void Fetch(Element element, object target, TypeCache typeCache, Cache cache)
{
if (target == null) return;
// Get all values first so if something goes wrong we haven't started modifying the object
List<KeyValuePair<PersistentMemberInfo, object>> values
= new List<KeyValuePair<PersistentMemberInfo, object>>();
foreach (PersistentMemberInfo memberInfo
in typeCache.PersistentMemberInfo.Where(m => m.Attribute.Fetch))
{
object member = memberInfo.GetValue(target);
TypeCache memberTypeCache = cache.GetTypeCache(memberInfo.Type);
object value = memberInfo.Attribute.FetchValue(element, member, memberTypeCache, cache);
if (memberInfo.Attribute.Required && value == null) throw new Exception("Could not get required member.");
values.Add(new KeyValuePair<PersistentMemberInfo, object>(memberInfo, value));
}
// Now that all conversions have been succesfully performed, set the values
foreach (KeyValuePair<PersistentMemberInfo, object> value in values)
{
value.Key.SetValue(target, value.Value);
}
// Call any custom logic if implemented
ICustomFetch custom = target as ICustomFetch;
if(custom != null)
{
custom.Fetch(element);
}
}
示例2: Serialize
internal override object Serialize(object source, TypeCache typeCache, Cache cache)
{
if (source == null) return null;
List<SerializedValue> values = new List<SerializedValue>();
// Do custom serialization
ICustomStore custom = source as ICustomStore;
if(custom != null)
{
values.Add(new SerializedValue(null, custom.Serialize(), null, null));
}
// Serialize the members
foreach (PersistentMemberInfo memberInfo
in typeCache.PersistentMemberInfo.Where(m => m.Attribute.Store))
{
object member = memberInfo.GetValue(source);
TypeCache memberTypeCache = cache.GetTypeCache(memberInfo.Type);
object serialized = memberInfo.Attribute.SerializeValue(member, memberTypeCache, cache);
values.Add(new SerializedValue(memberInfo.Attribute, serialized, member, memberTypeCache));
}
return values;
}
示例3: Inititalize
internal override void Inititalize(Type memberType, string memberName, Cache cache)
{
base.Inititalize(memberType, memberName, cache);
Name = GetName(Name, memberName, Query, CreateQuery);
ItemName = GetName(ItemName, "Item", ItemQuery);
// Get the TypeConverter
if(ItemTypeConverter != null)
{
if (ItemsArePersistentObjects) throw new Exception("A TypeConverter can not be specified for persistent member objects.");
_itemTypeConverter = InitializeTypeConverter(ItemTypeConverter);
}
// Resolve the type of collection and the item type
if (memberType.IsArray)
{
// It's an array, get the array item type
Type itemType = memberType.GetElementType();
if (itemType == null)
{
throw new Exception("Could not determine array item type.");
}
if (ItemType == null)
{
ItemType = itemType;
}
else if (!itemType.IsAssignableFrom(ItemType))
{
throw new Exception("The specified ItemType must be assignable to the array type.");
}
_getCollection = (c, s) =>
{
if (c == null || ((Array)c).Length != s) return Array.CreateInstance(ItemType, s);
return c;
};
_setCollectionItem = (c, i, v) => ((Array)c).SetValue(v, (Int64)i);
}
else
{
// Not an array, check interfaces
List<Type> collectionInterfaces = new List<Type>(memberType.GetInterfaces()
.Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>)));
if (collectionInterfaces.Count > 0)
{
// The member implements ICollection<T>, verify the ItemType (if specified) is compatable
Type collectionType = null;
foreach (Type collectionInterface in collectionInterfaces)
{
Type itemType = collectionInterface.GetGenericArguments()[0];
if (ItemType == null || itemType.IsAssignableFrom(ItemType))
{
if(ItemType == null) ItemType = itemType;
collectionType = collectionInterface;
break;
}
}
// Make sure we got an item type
if (collectionType == null) throw new Exception("No appropriate item type could be found.");
// Get the constructor and add functions
ConstructorInfo constructor = memberType.GetConstructor(Type.EmptyTypes);
if (constructor == null) throw new Exception("Persistent collection member must implement an empty constructor.");
MethodInfo clearMethod = collectionType.GetMethod("Clear");
_getCollection = (c, s) =>
{
if (c == null) return constructor.Invoke(null);
clearMethod.Invoke(c, null);
return c;
};
MethodInfo addMethod = collectionType.GetMethod("Add");
_setCollectionItem = (c, i, v) => addMethod.Invoke(c, new[] {v});
}
else if (typeof(IList).IsAssignableFrom(memberType))
{
// The member implements IList
if (ItemType == null) throw new Exception("A persistent collection that implements IList must provide an ItemType.");
ConstructorInfo constructor = memberType.GetConstructor(Type.EmptyTypes);
if (constructor == null) throw new Exception("Persistent collection member must implement an empty constructor.");
_getCollection = (c, s) =>
{
if( c == null) return constructor.Invoke(null);
((IList) c).Clear();
return c;
};
_setCollectionItem = (c, i, v) => ((IList) c).Add(v);
}
else
{
throw new Exception("Persistent collection member must be an array, implement ICollection<T>, or implement IList.");
}
}
_itemTypeCache = cache.GetTypeCache(ItemType);
}
示例4: Store
internal void Store(Element element, object serialized, Cache cache)
{
Initialize(cache);
_attribute.StoreValue(element, serialized, Collection, cache.GetTypeCache(_type), cache);
}
示例5: Serialize
internal object Serialize(Cache cache)
{
Initialize(cache);
return _attribute.SerializeValue(Collection, cache.GetTypeCache(_type), cache);
}
示例6: Fetch
internal void Fetch(Element element, Cache cache)
{
Initialize(cache);
Collection = _attribute.FetchValue(element, Collection, cache.GetTypeCache(_type), cache);
}
示例7: Inititalize
//.........这里部分代码省略.........
if (String.IsNullOrEmpty(ValueAttributeName))
{
ValueElementName = GetName(ValueElementName, "Value", ValueQuery);
}
// Get attribute names
if(KeysArePersistentObjects && !String.IsNullOrEmpty(KeyAttributeName))
throw new Exception("KeyAttributeName must not be specified if KeysArePersistentObjects is true.");
if (ValuesArePersistentObjects && !String.IsNullOrEmpty(ValueAttributeName))
throw new Exception("ValueAttributeName must not be specified if ValuesArePersistentObjects is true.");
// Get the TypeConverters
if (KeyTypeConverter != null)
{
if (KeysArePersistentObjects) throw new Exception("A TypeConverter can not be specified for persistent member objects.");
_keyTypeConverter = InitializeTypeConverter(KeyTypeConverter);
}
if (ValueTypeConverter != null)
{
if (ValuesArePersistentObjects) throw new Exception("A TypeConverter can not be specified for persistent member objects.");
_valueTypeConverter = InitializeTypeConverter(ValueTypeConverter);
}
// Resolve the type of collection and the key/value type
// Key = ICollection<>, Value = KeyValuePair<,>
List<KeyValuePair<Type, Type>> kvpInterfaces =
new List<KeyValuePair<Type, Type>>(memberType.GetInterfaces()
.Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>))
.Select(t => new KeyValuePair<Type, Type>(t, t.GetGenericArguments()[0]))
.Where(k => k.Value.IsGenericType && k.Value.GetGenericTypeDefinition() == typeof(KeyValuePair<,>)));
if(kvpInterfaces.Count > 0)
{
// The member implements ICollection<KeyValuePair<TKey,TValue>>
// Verify KeyType and ValueType are compatable (if specified)
KeyValuePair<Type, Type>? kvpType = null;
foreach (KeyValuePair<Type, Type> kvp in kvpInterfaces)
{
Type keyType = kvp.Value.GetGenericArguments()[0];
Type valueType = kvp.Value.GetGenericArguments()[1];
if((KeyType == null || keyType.IsAssignableFrom(KeyType))
&& (ValueType == null || valueType.IsAssignableFrom(ValueType)))
{
kvpType = kvp;
if(KeyType == null) KeyType = keyType;
if(ValueType == null) ValueType = valueType;
break;
}
}
// Make sure we got a kvp type
if (!kvpType.HasValue) throw new Exception("No appropriate key or value type could be found.");
// Get the constructor and other functions
ConstructorInfo constructor = memberType.GetConstructor(Type.EmptyTypes);
if (constructor == null) throw new Exception("Persistent collection member must implement an empty constructor.");
MethodInfo clearMethod = kvpType.Value.Key.GetMethod("Clear");
_getCollection = c =>
{
if (c == null) return constructor.Invoke(null);
clearMethod.Invoke(c, null);
return c;
};
ConstructorInfo kvpConstructor = kvpType.Value.Value.GetConstructor(new[] { KeyType, ValueType });
if (kvpConstructor == null) throw new Exception("Could not get KeyValuePair constructor.");
MethodInfo addMethod = kvpType.Value.Key.GetMethod("Add");
_setCollectionItem = (c, k, v) => addMethod.Invoke(c, new[] { kvpConstructor.Invoke(new []{k, v}) });
PropertyInfo keyProperty = kvpType.Value.Value.GetProperty("Key");
if(keyProperty == null) throw new Exception("Could not get Key property.");
_getKey = i => keyProperty.GetValue(i, null);
PropertyInfo valueProperty = kvpType.Value.Value.GetProperty("Value");
if (valueProperty == null) throw new Exception("Could not get Value property.");
_getValue = i => valueProperty.GetValue(i, null);
}
else if(typeof(IDictionary).IsAssignableFrom(memberType))
{
// The member implements IDictionary
if (KeyType == null) throw new Exception("A persistent collection that implements IDictionary must provide a KeyType.");
if (ValueType == null) throw new Exception("A persistent collection that implements IDictionary must provide a ValueType.");
ConstructorInfo constructor = memberType.GetConstructor(Type.EmptyTypes);
if (constructor == null) throw new Exception("Persistent collection member must implement an empty constructor.");
_getCollection = c =>
{
if (c == null) return constructor.Invoke(null);
((IDictionary)c).Clear();
return c;
};
_setCollectionItem = (c, k, v) => ((IDictionary)c).Add(k, v);
_getKey = i => ((DictionaryEntry)i).Key;
_getValue = i => ((DictionaryEntry)i).Value;
}
else
{
throw new Exception("Persistent kvp collection member must implement ICollection<KeyValuePair<TKey,TValue>> or implement IDictionary.");
}
_keyTypeCache = cache.GetTypeCache(KeyType);
_valueTypeCache = cache.GetTypeCache(ValueType);
}