internal LambdaExpression GetConstructor(Type t) { LambdaExpression e; ConstructorCache.TryGetValue(t, out e); return(e); }
private object DeserializeDictionary(Type type, BinaryReader stream) { var keyType = type.GenericTypeArguments[0]; var valueType = type.GenericTypeArguments[1]; CheckIfCanRead(keyType); CheckIfCanRead(valueType); var instance = ConstructorCache.Create(typeof(Dictionary <,>) .MakeGenericType(keyType, valueType)) as IDictionary; if (stream.BaseStream.CanSeek) { if (stream.BaseStream.Length == stream.BaseStream.Position) { return(instance); } } #pragma warning disable CS0168 try { int count = stream.ReadInt32(); if (count == 0) { return(instance); } for (int i = 0; i < count; i++) { instance.Add(readMethods[keyType](stream), readMethods[valueType](stream)); } } catch (EndOfStreamException ex) { } #pragma warning restore CS0168 return(instance); }
/// <summary> /// Returns the constructors for implementations of a particular interface /// type. Constructor info is cached after the initial crawl. /// </summary> /// <typeparam name="TInterface"></typeparam> /// <returns></returns> private IEnumerable <ConstructorInfo> GetConstructors <TInterface>() { if (ConstructorCache.ContainsKey(typeof(TInterface))) { return(ConstructorCache[typeof(TInterface)]); } else { var constructors = new LinkedList <ConstructorInfo>(); foreach (var asm in Assemblies) { foreach (var type in asm.GetTypes()) { if (type.IsClass && !type.IsAbstract) { if (type.GetInterfaces().Contains(typeof(TInterface))) { var constructor = type.GetConstructor(Type.EmptyTypes); constructors.AddLast(constructor); } } } } ConstructorCache[typeof(TInterface)] = constructors; return(constructors); } }
private object DeserializeList(Type type, XmlNode node) { var itemType = type.GenericTypeArguments[0]; var instance = ConstructorCache.Create(typeof(List <>).MakeGenericType(itemType)) as IList; Read(node, s => instance.Add(DeserializeSingle(itemType, s))); return(instance); }
public static bool HasDefaultConstructor([NotNull] this Type type) { if (type is null) { Throw.NullArgument(nameof(type)); } return(ConstructorCache.GetOrAddFor(type)); }
/// <summary> /// Parses a JSON string into the specified object type /// </summary> /// <param name="json">The JSON-encoded string you want to deserialize</param> /// <param name="type">The object type you want your json string deserialized into</param> /// <returns>Object of type Type</returns> public static object Deserialize(string json, Type type) { //Deserialize the json into a generic object object o = Deserialize(json); //Clear the Reflection caches PropertyCache.Clear(); ConstructorCache.Clear(); //Convert generic object to the specified Type return(Deserialize(o, type)); }
private static ConstructorInfo GetOrAddConstructorInfoFromCache(Type module) { ConstructorInfo ctor; if (ConstructorCache.TryGetValue(module, out ctor)) { return(ctor); } ctor = GetMostAppropriateConstructor(module); ConstructorCache.TryAdd(module, ctor); return(ctor); }
private object DeserializeList(Type type, BinaryReader stream) { var itemType = type.GenericTypeArguments[0]; CheckIfCanRead(itemType); var instance = ConstructorCache.Create(typeof(List <>).MakeGenericType(itemType)) as IList; var count = stream.ReadInt32(); Read(stream, count, itemType, v => instance.Add(v)); return(instance); }
public StyleVariant(XmlNode variantNode, ResourceManager resourceManager, ConstructorCache <IStyleAttribute> attributeCache) { // Set the name. Name = variantNode.Name; foreach (XmlNode childNode in variantNode) { // Create the attribute. IStyleAttribute styleAttribute = attributeCache.CreateInstance(childNode.Name, resourceManager, new AttributeCollection(childNode)); // Try to add the attributes to the collection. AddAttribute(styleAttribute); } }
/// <summary> Creates a new <see cref="UIManager"/> with the dependencies taken from the given <paramref name="game"/>. </summary> /// <param name="game"> The Monogame <see cref="Game"/> instance. </param> public UIManager(Game game) { // Set dependencies. if (game is null) { throw new ArgumentNullException("Given game cannot be null."); } contentManager = game.Content ?? throw new ArgumentNullException("Game's content manager was null."); gameWindow = game.Window ?? throw new ArgumentNullException("Game's window was null."); graphicsDevice = game.GraphicsDevice ?? throw new ArgumentNullException("Game's graphics device was null."); graphicsDevice.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents; // Create the element and component caches, loading the defaults. componentCache = new ConstructorCache <Component>(Assembly.GetExecutingAssembly(), "GuiCookie.Components"); elementCache = new ConstructorCache <Element>(Assembly.GetExecutingAssembly(), "GuiCookie.Elements"); }
public ElementManager(Root root, ComponentManager componentManager, TemplateManager templateManager, StyleManager styleManager, ConstructorCache <Element> elementCache, IServiceProvider serviceProvider) { // Set dependencies. this.root = root ?? throw new ArgumentNullException(nameof(root)); this.componentManager = componentManager ?? throw new ArgumentNullException(nameof(componentManager)); this.templateManager = templateManager ?? throw new ArgumentNullException(nameof(templateManager)); this.styleManager = styleManager ?? throw new ArgumentNullException(nameof(styleManager)); this.elementCache = elementCache ?? throw new ArgumentNullException(nameof(elementCache)); this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); // Initialise the elements list. ElementContainer = new ElementContainer(root); elementsByTag = new Dictionary <string, Element>(); // Bind to signals. //ElementContainer.OnChildAdded.Connect(addTaggedElement); ElementContainer.OnChildRemoved.Connect(removeTaggedElement); }
/// <summary> Creates a style from the given <paramref name="styleNode"/>. </summary> /// <param name="styleNode"> The <see cref="XmlNode"/> that contains the element style. </param> public Style(ResourceManager resourceManager, ConstructorCache <IStyleAttribute> attributeCache, XmlNode styleNode) { // Set the name of this style based on the name of the node. Name = styleNode.Name; // Create attributes from this style node. IReadOnlyAttributes attributes = new AttributeCollection(styleNode); // Set the name of the base style if one was given. BaseStyleName = attributes.GetAttributeOrDefault(BaseVariantName, string.Empty); // Hold collections of the loaded variants and attributes. List <StyleVariant> variants = new List <StyleVariant>(); List <IStyleAttribute> styleAttributes = new List <IStyleAttribute>(); // Read each child node. foreach (XmlNode childNode in styleNode) { // If the child node has children, load it as a variant. if (childNode.HasChildNodes) { variants.Add(new StyleVariant(childNode, resourceManager, attributeCache)); } // Otherwise; dynamically create the style attribute and add it to the list. else { styleAttributes.Add(attributeCache.CreateInstance(childNode.Name, resourceManager, new AttributeCollection(childNode))); } } // Create the base variant using the loaded attributes. BaseVariant = new StyleVariant(BaseVariantName, styleAttributes); AddVariant(BaseVariant); // Create the derived variants using the base variant. foreach (StyleVariant variant in variants) { // Combine the variant with the base. variant.CombineOverBase(BaseVariant); // Add the variant to the dictionary using its name. AddVariant(variant); } }
/// <summary> /// Returns the constructors for implementations of a particular interface /// type. Constructor info is cached after the initial crawl. /// </summary> /// <typeparam name="TInterface"></typeparam> /// <returns></returns> private IEnumerable <ConstructorInfo> GetConstructors <TInterface>() { if (ConstructorCache.ContainsKey(typeof(TInterface))) { return(ConstructorCache[typeof(TInterface)]); } else { LinkedList <ConstructorInfo> constructors = new LinkedList <ConstructorInfo>(); foreach (Assembly asm in Assemblies) { foreach (Type type in asm.GetTypes()) { if (type.IsClass && !type.IsAbstract) { if (type.GetInterfaces().Contains(typeof(TInterface))) { if (constructorArgs == null) { ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes); constructors.AddLast(constructor); } else { Type[] types = new Type[constructorArgs.Length]; for (int i = 0; i < constructorArgs.Length; i++) { types[i] = constructorArgs[i].GetType(); } ConstructorInfo constructor = type.GetConstructor(types); constructors.AddLast(constructor); } } } } } ConstructorCache[typeof(TInterface)] = constructors; return(constructors); } }
private object DeserializeDictionary(Type type, XmlNode node) { var keyType = type.GenericTypeArguments[0]; var valueType = type.GenericTypeArguments[1]; var instance = ConstructorCache.Create(typeof(Dictionary <,>) .MakeGenericType(keyType, valueType)) as IDictionary; if (!node.HasChildNodes) { return(instance); } foreach (XmlNode child in node.ChildNodes) { var key = child.Attributes["key"].Value; var value = child.Attributes["value"].Value; instance.Add(DeserializeSingle(keyType, key), DeserializeSingle(valueType, value)); } return(instance); }
/// <summary> /// Returns the constructors for implementations of a particular interface /// type. Constructor info is cached after the initial crawl. /// </summary> /// <typeparam name="TInterface"></typeparam> /// <returns></returns> private IEnumerable <ConstructorInfo> GetConstructors <TInterface>() { if (ConstructorCache.ContainsKey(typeof(TInterface))) { return(ConstructorCache[typeof(TInterface)]); } var constructors = new LinkedList <ConstructorInfo>(); foreach (ConstructorInfo constructor in Assemblies.SelectMany(asm => ( from type in asm.GetTypes() where type.IsClass && !type.IsAbstract && type.GetInterfaces().Contains(typeof(TInterface)) select type.GetConstructor(Type.EmptyTypes)))) { constructors.AddLast(constructor); } ConstructorCache[typeof(TInterface)] = constructors; return(constructors); }
/// <summary> /// Constructor for the <c>Instantiator</c> object. This will /// create a constructor cache that can be used to cache all of /// the constructors instantiated for the required types. /// </summary> public Instantiator() { this.cache = new ConstructorCache(); }
public static bool HasDefaultConstructor(this Type type) => ConstructorCache.GetOrAddFor(Ensure.NotNull(type));
internal ComponentManager(ConstructorCache <Component> componentCache, IServiceProvider serviceProvider) { // Set dependencies. this.componentCache = componentCache ?? throw new ArgumentNullException(nameof(componentCache)); this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); }
/// <summary> /// Clears all plugin assemblies and type info. /// </summary> public void UnInit() { Assemblies.Clear(); ConstructorCache.Clear(); }
public static T Create <T>(GameObject obj) where T : GOBase { return(ConstructorCache <T> .Constructor(obj)); }