Exemplo n.º 1
0
    /// <summary>
    /// Get a template by type from the templates warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse.
    /// </summary>
    /// <param name="type">.Net type.</param>
    /// <returns>Resource template.</returns>
    public static TypeTemplate GetTemplateByType(Type type)
    {
        TemplateType templateType = TemplateType.Unspecified;

        if (Codec.InheritsClass(type, typeof(DistributedResource)))
        {
            templateType = TemplateType.Wrapper;
        }
        if (Codec.ImplementsInterface(type, typeof(IResource)))
        {
            templateType = TemplateType.Resource;
        }
        else if (Codec.ImplementsInterface(type, typeof(IRecord)))
        {
            templateType = TemplateType.Record;
        }
        else if (type.IsEnum)
        {
            templateType = TemplateType.Enum;
        }
        else
        {
            return(null);
        }

        var baseType = ResourceProxy.GetBaseType(type);

        if (baseType == typeof(IResource) ||
            baseType == typeof(IRecord))
        {
            return(null);
        }

        var template = templates[templateType].Values.FirstOrDefault(x => x.DefinedType == type);

        // loaded ?
        if (template == null)
        {
            template = new TypeTemplate(baseType, true);
            TypeTemplate.GetDependencies(template);
        }

        return(template);
    }