初始化
This commit is contained in:
@@ -0,0 +1,293 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>
|
||||
</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{749E71B2-D02B-439F-A8D3-623434B7270A}</ProjectGuid>
|
||||
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>BLV_API</RootNamespace>
|
||||
<AssemblyName>BLV_API</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<MvcBuildViews>false</MvcBuildViews>
|
||||
<UseIISExpress>true</UseIISExpress>
|
||||
<Use64BitIISExpress />
|
||||
<IISExpressSSLPort />
|
||||
<IISExpressAnonymousAuthentication />
|
||||
<IISExpressWindowsAuthentication />
|
||||
<IISExpressUseClassicPipelineMode />
|
||||
<UseGlobalApplicationHostFile />
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Web.Entity" />
|
||||
<Reference Include="System.Web.ApplicationServices" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="System.Web.Extensions" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Abstractions" />
|
||||
<Reference Include="System.Web.Routing" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Net.Http">
|
||||
</Reference>
|
||||
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Net.Http.WebRequest">
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.Helpers.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.7\lib\net45\System.Web.Http.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http.WebHost, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.7\lib\net45\System.Web.Http.WebHost.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Optimization">
|
||||
<HintPath>..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.7\lib\net45\System.Web.Razor.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.WebPages.Deployment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="WebGrease">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\WebGrease.1.6.0\lib\WebGrease.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Antlr3.Runtime">
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform">
|
||||
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="App_Start\BundleConfig.cs" />
|
||||
<Compile Include="App_Start\FilterConfig.cs" />
|
||||
<Compile Include="App_Start\RouteConfig.cs" />
|
||||
<Compile Include="App_Start\WebApiConfig.cs" />
|
||||
<Compile Include="Areas\HelpPage\ApiDescriptionExtensions.cs" />
|
||||
<Compile Include="Areas\HelpPage\App_Start\HelpPageConfig.cs" />
|
||||
<Compile Include="Areas\HelpPage\Controllers\HelpController.cs" />
|
||||
<Compile Include="Areas\HelpPage\HelpPageAreaRegistration.cs" />
|
||||
<Compile Include="Areas\HelpPage\HelpPageConfigurationExtensions.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\CollectionModelDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\ComplexTypeModelDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\DictionaryModelDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\EnumTypeModelDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\EnumValueDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\IModelDocumentationProvider.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\KeyValuePairModelDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\ModelDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\ModelDescriptionGenerator.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\ModelNameAttribute.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\ModelNameHelper.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\ParameterAnnotation.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\ParameterDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\ModelDescriptions\SimpleTypeModelDescription.cs" />
|
||||
<Compile Include="Areas\HelpPage\Models\HelpPageApiModel.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\HelpPageSampleGenerator.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\HelpPageSampleKey.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\ImageSample.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\InvalidSample.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\ObjectGenerator.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\SampleDirection.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\TextSample.cs" />
|
||||
<Compile Include="Areas\HelpPage\XmlDocumentationProvider.cs" />
|
||||
<Compile Include="Controllers\HomeController.cs" />
|
||||
<Compile Include="Controllers\ValuesController.cs" />
|
||||
<Compile Include="Dal\DbProvider.cs" />
|
||||
<Compile Include="Dal\SqlHelper.cs" />
|
||||
<Compile Include="Global.asax.cs">
|
||||
<DependentUpon>Global.asax</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Tools.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Areas\HelpPage\HelpPage.css" />
|
||||
<Content Include="Content\bootstrap-theme.css" />
|
||||
<Content Include="Content\bootstrap-theme.min.css" />
|
||||
<Content Include="Content\bootstrap.css" />
|
||||
<Content Include="Content\bootstrap.min.css" />
|
||||
<Content Include="favicon.ico" />
|
||||
<Content Include="fonts\glyphicons-halflings-regular.svg" />
|
||||
<Content Include="Global.asax" />
|
||||
<Content Include="Scripts\bootstrap.js" />
|
||||
<Content Include="Scripts\bootstrap.min.js" />
|
||||
<Content Include="Areas\HelpPage\Views\Web.config" />
|
||||
<Content Include="Areas\HelpPage\Views\Shared\_Layout.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\ResourceModel.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\Index.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\TextSample.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\SimpleTypeModelDescription.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\Samples.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\Parameters.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\ModelDescriptionLink.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\KeyValuePairModelDescription.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\InvalidSample.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\ImageSample.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\HelpPageApiModel.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\EnumTypeModelDescription.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\DictionaryModelDescription.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\ComplexTypeModelDescription.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\CollectionModelDescription.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\DisplayTemplates\ApiGroup.cshtml" />
|
||||
<Content Include="Areas\HelpPage\Views\Help\Api.cshtml" />
|
||||
<None Include="Scripts\jquery-3.4.1.intellisense.js" />
|
||||
<Content Include="Scripts\jquery-3.4.1.js" />
|
||||
<Content Include="Scripts\jquery-3.4.1.min.js" />
|
||||
<Content Include="Scripts\jquery-3.4.1.slim.js" />
|
||||
<Content Include="Scripts\jquery-3.4.1.slim.min.js" />
|
||||
<Content Include="Scripts\modernizr-2.8.3.js" />
|
||||
<Content Include="Web.config" />
|
||||
<Content Include="Web.Debug.config">
|
||||
<DependentUpon>Web.config</DependentUpon>
|
||||
</Content>
|
||||
<Content Include="Web.Release.config">
|
||||
<DependentUpon>Web.config</DependentUpon>
|
||||
</Content>
|
||||
<Content Include="Areas\HelpPage\Views\_ViewStart.cshtml" />
|
||||
<Content Include="Content\Site.css" />
|
||||
<Content Include="Views\Web.config" />
|
||||
<Content Include="Views\_ViewStart.cshtml" />
|
||||
<Content Include="Views\Home\Index.cshtml" />
|
||||
<Content Include="Views\Shared\Error.cshtml" />
|
||||
<Content Include="Views\Shared\_Layout.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="App_Data\" />
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="fonts\glyphicons-halflings-regular.woff2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="fonts\glyphicons-halflings-regular.woff" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="fonts\glyphicons-halflings-regular.ttf" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="fonts\glyphicons-halflings-regular.eot" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Content\bootstrap.min.css.map" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Content\bootstrap.css.map" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Content\bootstrap-theme.min.css.map" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Content\bootstrap-theme.css.map" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
<Content Include="Scripts\jquery-3.4.1.slim.min.map" />
|
||||
<Content Include="Scripts\jquery-3.4.1.min.map" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
|
||||
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
|
||||
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
|
||||
</Target>
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
|
||||
<WebProjectProperties>
|
||||
<UseIIS>True</UseIIS>
|
||||
<AutoAssignPort>True</AutoAssignPort>
|
||||
<DevelopmentServerPort>58019</DevelopmentServerPort>
|
||||
<DevelopmentServerVPath>/</DevelopmentServerVPath>
|
||||
<IISUrl>http://localhost:58019/</IISUrl>
|
||||
<NTLMAuthentication>False</NTLMAuthentication>
|
||||
<UseCustomServer>False</UseCustomServer>
|
||||
<CustomServerUrl>
|
||||
</CustomServerUrl>
|
||||
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
|
||||
</WebProjectProperties>
|
||||
</FlavorProperties>
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
|
||||
</Target>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target> -->
|
||||
</Project>
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Optimization;
|
||||
using System.Web.Routing;
|
||||
|
||||
namespace BLV_API
|
||||
{
|
||||
public class WebApiApplication : System.Web.HttpApplication
|
||||
{
|
||||
protected void Application_Start()
|
||||
{
|
||||
AreaRegistration.RegisterAllAreas();
|
||||
GlobalConfiguration.Configure(WebApiConfig.Register);
|
||||
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
|
||||
RouteConfig.RegisterRoutes(RouteTable.Routes);
|
||||
BundleConfig.RegisterBundles(BundleTable.Bundles);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
@using PTS_API_V1.Areas.HelpPage
|
||||
@model ImageSample
|
||||
|
||||
<img src="@Model.Src" />
|
||||
@@ -0,0 +1,456 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace PTS_API_V1.Areas.HelpPage
|
||||
{
|
||||
/// <summary>
|
||||
/// This class will create an object of a given type and populate it with sample data.
|
||||
/// </summary>
|
||||
public class ObjectGenerator
|
||||
{
|
||||
internal const int DefaultCollectionSize = 2;
|
||||
private readonly SimpleTypeObjectGenerator SimpleObjectGenerator = new SimpleTypeObjectGenerator();
|
||||
|
||||
/// <summary>
|
||||
/// Generates an object for a given type. The type needs to be public, have a public default constructor and settable public properties/fields. Currently it supports the following types:
|
||||
/// Simple types: <see cref="int"/>, <see cref="string"/>, <see cref="Enum"/>, <see cref="DateTime"/>, <see cref="Uri"/>, etc.
|
||||
/// Complex types: POCO types.
|
||||
/// Nullables: <see cref="Nullable{T}"/>.
|
||||
/// Arrays: arrays of simple types or complex types.
|
||||
/// Key value pairs: <see cref="KeyValuePair{TKey,TValue}"/>
|
||||
/// Tuples: <see cref="Tuple{T1}"/>, <see cref="Tuple{T1,T2}"/>, etc
|
||||
/// Dictionaries: <see cref="IDictionary{TKey,TValue}"/> or anything deriving from <see cref="IDictionary{TKey,TValue}"/>.
|
||||
/// Collections: <see cref="IList{T}"/>, <see cref="IEnumerable{T}"/>, <see cref="ICollection{T}"/>, <see cref="IList"/>, <see cref="IEnumerable"/>, <see cref="ICollection"/> or anything deriving from <see cref="ICollection{T}"/> or <see cref="IList"/>.
|
||||
/// Queryables: <see cref="IQueryable"/>, <see cref="IQueryable{T}"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <returns>An object of the given type.</returns>
|
||||
public object GenerateObject(Type type)
|
||||
{
|
||||
return GenerateObject(type, new Dictionary<Type, object>());
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Here we just want to return null if anything goes wrong.")]
|
||||
private object GenerateObject(Type type, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SimpleTypeObjectGenerator.CanGenerateObject(type))
|
||||
{
|
||||
return SimpleObjectGenerator.GenerateObject(type);
|
||||
}
|
||||
|
||||
if (type.IsArray)
|
||||
{
|
||||
return GenerateArray(type, DefaultCollectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
return GenerateGenericType(type, DefaultCollectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (type == typeof(IDictionary))
|
||||
{
|
||||
return GenerateDictionary(typeof(Hashtable), DefaultCollectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (typeof(IDictionary).IsAssignableFrom(type))
|
||||
{
|
||||
return GenerateDictionary(type, DefaultCollectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (type == typeof(IList) ||
|
||||
type == typeof(IEnumerable) ||
|
||||
type == typeof(ICollection))
|
||||
{
|
||||
return GenerateCollection(typeof(ArrayList), DefaultCollectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (typeof(IList).IsAssignableFrom(type))
|
||||
{
|
||||
return GenerateCollection(type, DefaultCollectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (type == typeof(IQueryable))
|
||||
{
|
||||
return GenerateQueryable(type, DefaultCollectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (type.IsEnum)
|
||||
{
|
||||
return GenerateEnum(type);
|
||||
}
|
||||
|
||||
if (type.IsPublic || type.IsNestedPublic)
|
||||
{
|
||||
return GenerateComplexObject(type, createdObjectReferences);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Returns null if anything fails
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static object GenerateGenericType(Type type, int collectionSize, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
Type genericTypeDefinition = type.GetGenericTypeDefinition();
|
||||
if (genericTypeDefinition == typeof(Nullable<>))
|
||||
{
|
||||
return GenerateNullable(type, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (genericTypeDefinition == typeof(KeyValuePair<,>))
|
||||
{
|
||||
return GenerateKeyValuePair(type, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (IsTuple(genericTypeDefinition))
|
||||
{
|
||||
return GenerateTuple(type, createdObjectReferences);
|
||||
}
|
||||
|
||||
Type[] genericArguments = type.GetGenericArguments();
|
||||
if (genericArguments.Length == 1)
|
||||
{
|
||||
if (genericTypeDefinition == typeof(IList<>) ||
|
||||
genericTypeDefinition == typeof(IEnumerable<>) ||
|
||||
genericTypeDefinition == typeof(ICollection<>))
|
||||
{
|
||||
Type collectionType = typeof(List<>).MakeGenericType(genericArguments);
|
||||
return GenerateCollection(collectionType, collectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
if (genericTypeDefinition == typeof(IQueryable<>))
|
||||
{
|
||||
return GenerateQueryable(type, collectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
Type closedCollectionType = typeof(ICollection<>).MakeGenericType(genericArguments[0]);
|
||||
if (closedCollectionType.IsAssignableFrom(type))
|
||||
{
|
||||
return GenerateCollection(type, collectionSize, createdObjectReferences);
|
||||
}
|
||||
}
|
||||
|
||||
if (genericArguments.Length == 2)
|
||||
{
|
||||
if (genericTypeDefinition == typeof(IDictionary<,>))
|
||||
{
|
||||
Type dictionaryType = typeof(Dictionary<,>).MakeGenericType(genericArguments);
|
||||
return GenerateDictionary(dictionaryType, collectionSize, createdObjectReferences);
|
||||
}
|
||||
|
||||
Type closedDictionaryType = typeof(IDictionary<,>).MakeGenericType(genericArguments[0], genericArguments[1]);
|
||||
if (closedDictionaryType.IsAssignableFrom(type))
|
||||
{
|
||||
return GenerateDictionary(type, collectionSize, createdObjectReferences);
|
||||
}
|
||||
}
|
||||
|
||||
if (type.IsPublic || type.IsNestedPublic)
|
||||
{
|
||||
return GenerateComplexObject(type, createdObjectReferences);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static object GenerateTuple(Type type, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
Type[] genericArgs = type.GetGenericArguments();
|
||||
object[] parameterValues = new object[genericArgs.Length];
|
||||
bool failedToCreateTuple = true;
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
for (int i = 0; i < genericArgs.Length; i++)
|
||||
{
|
||||
parameterValues[i] = objectGenerator.GenerateObject(genericArgs[i], createdObjectReferences);
|
||||
failedToCreateTuple &= parameterValues[i] == null;
|
||||
}
|
||||
if (failedToCreateTuple)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
object result = Activator.CreateInstance(type, parameterValues);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static bool IsTuple(Type genericTypeDefinition)
|
||||
{
|
||||
return genericTypeDefinition == typeof(Tuple<>) ||
|
||||
genericTypeDefinition == typeof(Tuple<,>) ||
|
||||
genericTypeDefinition == typeof(Tuple<,,>) ||
|
||||
genericTypeDefinition == typeof(Tuple<,,,>) ||
|
||||
genericTypeDefinition == typeof(Tuple<,,,,>) ||
|
||||
genericTypeDefinition == typeof(Tuple<,,,,,>) ||
|
||||
genericTypeDefinition == typeof(Tuple<,,,,,,>) ||
|
||||
genericTypeDefinition == typeof(Tuple<,,,,,,,>);
|
||||
}
|
||||
|
||||
private static object GenerateKeyValuePair(Type keyValuePairType, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
Type[] genericArgs = keyValuePairType.GetGenericArguments();
|
||||
Type typeK = genericArgs[0];
|
||||
Type typeV = genericArgs[1];
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
object keyObject = objectGenerator.GenerateObject(typeK, createdObjectReferences);
|
||||
object valueObject = objectGenerator.GenerateObject(typeV, createdObjectReferences);
|
||||
if (keyObject == null && valueObject == null)
|
||||
{
|
||||
// Failed to create key and values
|
||||
return null;
|
||||
}
|
||||
object result = Activator.CreateInstance(keyValuePairType, keyObject, valueObject);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static object GenerateArray(Type arrayType, int size, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
Type type = arrayType.GetElementType();
|
||||
Array result = Array.CreateInstance(type, size);
|
||||
bool areAllElementsNull = true;
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
object element = objectGenerator.GenerateObject(type, createdObjectReferences);
|
||||
result.SetValue(element, i);
|
||||
areAllElementsNull &= element == null;
|
||||
}
|
||||
|
||||
if (areAllElementsNull)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static object GenerateDictionary(Type dictionaryType, int size, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
Type typeK = typeof(object);
|
||||
Type typeV = typeof(object);
|
||||
if (dictionaryType.IsGenericType)
|
||||
{
|
||||
Type[] genericArgs = dictionaryType.GetGenericArguments();
|
||||
typeK = genericArgs[0];
|
||||
typeV = genericArgs[1];
|
||||
}
|
||||
|
||||
object result = Activator.CreateInstance(dictionaryType);
|
||||
MethodInfo addMethod = dictionaryType.GetMethod("Add") ?? dictionaryType.GetMethod("TryAdd");
|
||||
MethodInfo containsMethod = dictionaryType.GetMethod("Contains") ?? dictionaryType.GetMethod("ContainsKey");
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
object newKey = objectGenerator.GenerateObject(typeK, createdObjectReferences);
|
||||
if (newKey == null)
|
||||
{
|
||||
// Cannot generate a valid key
|
||||
return null;
|
||||
}
|
||||
|
||||
bool containsKey = (bool)containsMethod.Invoke(result, new object[] { newKey });
|
||||
if (!containsKey)
|
||||
{
|
||||
object newValue = objectGenerator.GenerateObject(typeV, createdObjectReferences);
|
||||
addMethod.Invoke(result, new object[] { newKey, newValue });
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static object GenerateEnum(Type enumType)
|
||||
{
|
||||
Array possibleValues = Enum.GetValues(enumType);
|
||||
if (possibleValues.Length > 0)
|
||||
{
|
||||
return possibleValues.GetValue(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static object GenerateQueryable(Type queryableType, int size, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
bool isGeneric = queryableType.IsGenericType;
|
||||
object list;
|
||||
if (isGeneric)
|
||||
{
|
||||
Type listType = typeof(List<>).MakeGenericType(queryableType.GetGenericArguments());
|
||||
list = GenerateCollection(listType, size, createdObjectReferences);
|
||||
}
|
||||
else
|
||||
{
|
||||
list = GenerateArray(typeof(object[]), size, createdObjectReferences);
|
||||
}
|
||||
if (list == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (isGeneric)
|
||||
{
|
||||
Type argumentType = typeof(IEnumerable<>).MakeGenericType(queryableType.GetGenericArguments());
|
||||
MethodInfo asQueryableMethod = typeof(Queryable).GetMethod("AsQueryable", new[] { argumentType });
|
||||
return asQueryableMethod.Invoke(null, new[] { list });
|
||||
}
|
||||
|
||||
return Queryable.AsQueryable((IEnumerable)list);
|
||||
}
|
||||
|
||||
private static object GenerateCollection(Type collectionType, int size, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
Type type = collectionType.IsGenericType ?
|
||||
collectionType.GetGenericArguments()[0] :
|
||||
typeof(object);
|
||||
object result = Activator.CreateInstance(collectionType);
|
||||
MethodInfo addMethod = collectionType.GetMethod("Add");
|
||||
bool areAllElementsNull = true;
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
object element = objectGenerator.GenerateObject(type, createdObjectReferences);
|
||||
addMethod.Invoke(result, new object[] { element });
|
||||
areAllElementsNull &= element == null;
|
||||
}
|
||||
|
||||
if (areAllElementsNull)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static object GenerateNullable(Type nullableType, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
Type type = nullableType.GetGenericArguments()[0];
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
return objectGenerator.GenerateObject(type, createdObjectReferences);
|
||||
}
|
||||
|
||||
private static object GenerateComplexObject(Type type, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
object result = null;
|
||||
|
||||
if (createdObjectReferences.TryGetValue(type, out result))
|
||||
{
|
||||
// The object has been created already, just return it. This will handle the circular reference case.
|
||||
return result;
|
||||
}
|
||||
|
||||
if (type.IsValueType)
|
||||
{
|
||||
result = Activator.CreateInstance(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
ConstructorInfo defaultCtor = type.GetConstructor(Type.EmptyTypes);
|
||||
if (defaultCtor == null)
|
||||
{
|
||||
// Cannot instantiate the type because it doesn't have a default constructor
|
||||
return null;
|
||||
}
|
||||
|
||||
result = defaultCtor.Invoke(new object[0]);
|
||||
}
|
||||
createdObjectReferences.Add(type, result);
|
||||
SetPublicProperties(type, result, createdObjectReferences);
|
||||
SetPublicFields(type, result, createdObjectReferences);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void SetPublicProperties(Type type, object obj, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
foreach (PropertyInfo property in properties)
|
||||
{
|
||||
if (property.CanWrite)
|
||||
{
|
||||
object propertyValue = objectGenerator.GenerateObject(property.PropertyType, createdObjectReferences);
|
||||
property.SetValue(obj, propertyValue, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetPublicFields(Type type, object obj, Dictionary<Type, object> createdObjectReferences)
|
||||
{
|
||||
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
|
||||
ObjectGenerator objectGenerator = new ObjectGenerator();
|
||||
foreach (FieldInfo field in fields)
|
||||
{
|
||||
object fieldValue = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences);
|
||||
field.SetValue(obj, fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
private class SimpleTypeObjectGenerator
|
||||
{
|
||||
private long _index = 0;
|
||||
private static readonly Dictionary<Type, Func<long, object>> DefaultGenerators = InitializeGenerators();
|
||||
|
||||
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "These are simple type factories and cannot be split up.")]
|
||||
private static Dictionary<Type, Func<long, object>> InitializeGenerators()
|
||||
{
|
||||
return new Dictionary<Type, Func<long, object>>
|
||||
{
|
||||
{ typeof(Boolean), index => true },
|
||||
{ typeof(Byte), index => (Byte)64 },
|
||||
{ typeof(Char), index => (Char)65 },
|
||||
{ typeof(DateTime), index => DateTime.Now },
|
||||
{ typeof(DateTimeOffset), index => new DateTimeOffset(DateTime.Now) },
|
||||
{ typeof(DBNull), index => DBNull.Value },
|
||||
{ typeof(Decimal), index => (Decimal)index },
|
||||
{ typeof(Double), index => (Double)(index + 0.1) },
|
||||
{ typeof(Guid), index => Guid.NewGuid() },
|
||||
{ typeof(Int16), index => (Int16)(index % Int16.MaxValue) },
|
||||
{ typeof(Int32), index => (Int32)(index % Int32.MaxValue) },
|
||||
{ typeof(Int64), index => (Int64)index },
|
||||
{ typeof(Object), index => new object() },
|
||||
{ typeof(SByte), index => (SByte)64 },
|
||||
{ typeof(Single), index => (Single)(index + 0.1) },
|
||||
{
|
||||
typeof(String), index =>
|
||||
{
|
||||
return String.Format(CultureInfo.CurrentCulture, "sample string {0}", index);
|
||||
}
|
||||
},
|
||||
{
|
||||
typeof(TimeSpan), index =>
|
||||
{
|
||||
return TimeSpan.FromTicks(1234567);
|
||||
}
|
||||
},
|
||||
{ typeof(UInt16), index => (UInt16)(index % UInt16.MaxValue) },
|
||||
{ typeof(UInt32), index => (UInt32)(index % UInt32.MaxValue) },
|
||||
{ typeof(UInt64), index => (UInt64)index },
|
||||
{
|
||||
typeof(Uri), index =>
|
||||
{
|
||||
return new Uri(String.Format(CultureInfo.CurrentCulture, "http://webapihelppage{0}.com", index));
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public static bool CanGenerateObject(Type type)
|
||||
{
|
||||
return DefaultGenerators.ContainsKey(type);
|
||||
}
|
||||
|
||||
public object GenerateObject(Type type)
|
||||
{
|
||||
return DefaultGenerators[type](++_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user