add sample.
This commit is contained in:
22
src/IoC_Inject/IoC_Inject.sln
Normal file
22
src/IoC_Inject/IoC_Inject.sln
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.25420.1
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IoC_Inject", "IoC_Inject\IoC_Inject.csproj", "{D4DEBE99-D920-4391-9042-E957F4BC0303}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{D4DEBE99-D920-4391-9042-E957F4BC0303}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D4DEBE99-D920-4391-9042-E957F4BC0303}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D4DEBE99-D920-4391-9042-E957F4BC0303}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D4DEBE99-D920-4391-9042-E957F4BC0303}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
2
src/IoC_Inject/IoC_Inject.sln.DotSettings.user
Normal file
2
src/IoC_Inject/IoC_Inject.sln.DotSettings.user
Normal file
@@ -0,0 +1,2 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Int64 x:Key="/Default/CodeStyle/Naming/CSharpAutoNaming/AutoNamingCompletedVersion/@EntryValue">2</s:Int64></wpf:ResourceDictionary>
|
||||
6
src/IoC_Inject/IoC_Inject/App.config
Normal file
6
src/IoC_Inject/IoC_Inject/App.config
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
|
||||
</startup>
|
||||
</configuration>
|
||||
65
src/IoC_Inject/IoC_Inject/IoC_Inject.csproj
Normal file
65
src/IoC_Inject/IoC_Inject/IoC_Inject.csproj
Normal file
@@ -0,0 +1,65 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<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>
|
||||
<ProjectGuid>{D4DEBE99-D920-4391-9042-E957F4BC0303}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>IoC_Inject</RootNamespace>
|
||||
<AssemblyName>IoC_Inject</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Autofac, Version=4.9.2.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Autofac.4.9.2\lib\net45\Autofac.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- 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>
|
||||
76
src/IoC_Inject/IoC_Inject/Program.cs
Normal file
76
src/IoC_Inject/IoC_Inject/Program.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Autofac;
|
||||
|
||||
namespace IoC_Inject
|
||||
{
|
||||
public interface IWalkable
|
||||
{
|
||||
void Walk();
|
||||
|
||||
void Walk(TextWriter writer);
|
||||
}
|
||||
|
||||
public class Person : IWalkable
|
||||
{
|
||||
private readonly TextWriter _writer;
|
||||
|
||||
public TextWriter WriterProp { get; set; }
|
||||
|
||||
|
||||
public Person(TextWriter w)
|
||||
{
|
||||
_writer = w;
|
||||
}
|
||||
|
||||
public void Walk()
|
||||
{
|
||||
_writer.WriteLine("constructor walk");
|
||||
WriterProp.WriteLine("property walk");
|
||||
}
|
||||
|
||||
public void Walk(TextWriter writer)
|
||||
{
|
||||
writer.WriteLine("walk by parameter");
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
IContainer container = GetContainer();
|
||||
|
||||
var w= container.Resolve<IWalkable>();
|
||||
w.Walk();
|
||||
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
private static IContainer GetContainer()
|
||||
{
|
||||
ContainerBuilder builder = new ContainerBuilder();
|
||||
|
||||
builder.Register(x => Console.Out).As<TextWriter>();
|
||||
|
||||
builder.RegisterType<Person>().As<IWalkable>();
|
||||
|
||||
|
||||
//註冊屬性和方法參數
|
||||
builder.RegisterType<Person>()
|
||||
.As<IWalkable>().OnActivating(e =>
|
||||
{
|
||||
var writer = e.Context.Resolve<TextWriter>();
|
||||
e.Instance.WriterProp = writer;
|
||||
e.Instance.Walk(writer);
|
||||
});
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/IoC_Inject/IoC_Inject/Properties/AssemblyInfo.cs
Normal file
36
src/IoC_Inject/IoC_Inject/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("IoC_Inject")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("IoC_Inject")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2019")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("d4debe99-d920-4391-9042-e957f4bc0303")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
4
src/IoC_Inject/IoC_Inject/packages.config
Normal file
4
src/IoC_Inject/IoC_Inject/packages.config
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Autofac" version="4.9.2" targetFramework="net462" />
|
||||
</packages>
|
||||
BIN
src/IoC_Inject/packages/Autofac.4.9.2/.signature.p7s
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/.signature.p7s
vendored
Normal file
Binary file not shown.
BIN
src/IoC_Inject/packages/Autofac.4.9.2/Autofac.4.9.2.nupkg
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/Autofac.4.9.2.nupkg
vendored
Normal file
Binary file not shown.
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/net45/Autofac.dll
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/net45/Autofac.dll
vendored
Normal file
Binary file not shown.
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/net45/Autofac.pdb
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/net45/Autofac.pdb
vendored
Normal file
Binary file not shown.
8177
src/IoC_Inject/packages/Autofac.4.9.2/lib/net45/Autofac.xml
vendored
Normal file
8177
src/IoC_Inject/packages/Autofac.4.9.2/lib/net45/Autofac.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard1.1/Autofac.dll
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard1.1/Autofac.dll
vendored
Normal file
Binary file not shown.
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard1.1/Autofac.pdb
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard1.1/Autofac.pdb
vendored
Normal file
Binary file not shown.
8177
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard1.1/Autofac.xml
vendored
Normal file
8177
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard1.1/Autofac.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard2.0/Autofac.dll
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard2.0/Autofac.dll
vendored
Normal file
Binary file not shown.
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard2.0/Autofac.pdb
vendored
Normal file
BIN
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard2.0/Autofac.pdb
vendored
Normal file
Binary file not shown.
8177
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard2.0/Autofac.xml
vendored
Normal file
8177
src/IoC_Inject/packages/Autofac.4.9.2/lib/netstandard2.0/Autofac.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user