Skip to content

Commit 0bedb46

Browse files
committed
resolved #125
1 parent 122ccf4 commit 0bedb46

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/CatLib.Core.Tests/Support/Container/ContainerTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
using System;
13+
using System.CodeDom;
1314
using System.Collections.Generic;
1415
using System.Reflection;
1516
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -2715,6 +2716,75 @@ public void TestNeedGivenWithParamName()
27152716

27162717
Assert.AreEqual(200, container.Make<TestNeedGivenWithParamNameClass>().MyParam);
27172718
}
2719+
2720+
[TestMethod]
2721+
public void TestNullRelease()
2722+
{
2723+
var container = new Container();
2724+
Assert.AreEqual(false, container.Release(null));
2725+
}
2726+
2727+
public class TestGivenInvalidTypeClass
2728+
{
2729+
public TestGivenInvalidTypeClass(Container container)
2730+
{
2731+
Assert.Fail();
2732+
}
2733+
}
2734+
2735+
[TestMethod]
2736+
[ExpectedException(typeof(UnresolvableException))]
2737+
public void TestGivenInvalidType()
2738+
{
2739+
var container = new Container();
2740+
container.Bind<TestGivenInvalidTypeClass>()
2741+
.Needs("$container").Given(() => 123);
2742+
container.Make<TestGivenInvalidTypeClass>();
2743+
}
2744+
2745+
public class TestGivenInvalidTypeAttrClass
2746+
{
2747+
[Inject]
2748+
public Container container { get; set; }
2749+
}
2750+
2751+
[TestMethod]
2752+
[ExpectedException(typeof(UnresolvableException))]
2753+
public void TestGivenInvalidTypeAttr()
2754+
{
2755+
var container = new Container();
2756+
container.Bind<TestGivenInvalidTypeAttrClass>()
2757+
.Needs("$container").Given(() => 123);
2758+
container.Make<TestGivenInvalidTypeAttrClass>();
2759+
}
2760+
2761+
public class NotSupportNullInject : Container
2762+
{
2763+
protected override bool CanInject(Type type, object instance)
2764+
{
2765+
return instance != null;
2766+
}
2767+
}
2768+
2769+
[TestMethod]
2770+
[ExpectedException(typeof(UnresolvableException))]
2771+
public void TestGivenInvalidTypeAttrNotSupportNullInject()
2772+
{
2773+
var container = new NotSupportNullInject();
2774+
container.Bind<TestGivenInvalidTypeAttrClass>()
2775+
.Needs("$container").Given(() => null);
2776+
container.Make<TestGivenInvalidTypeAttrClass>();
2777+
}
2778+
2779+
[TestMethod]
2780+
[ExpectedException(typeof(UnresolvableException))]
2781+
public void TestGivenInvalidTypeNotSupportNullInject()
2782+
{
2783+
var container = new NotSupportNullInject();
2784+
container.Bind<TestGivenInvalidTypeClass>()
2785+
.Needs("$container").Given(() => null);
2786+
container.Make<TestGivenInvalidTypeClass>();
2787+
}
27182788
#endregion
27192789

27202790
/// <summary>

src/CatLib.Core/Support/Container/Container.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ protected virtual void AttributeInject(Bindable makeServiceBindData, object make
14961496
if (!CanInject(property.PropertyType, instance))
14971497
{
14981498
throw new UnresolvableException(
1499-
$"[{makeServiceBindData.Service}]({makeServiceInstance.GetType()}) Attr inject type must be [{property.PropertyType}] , But instance is [{instance.GetType()}] , Make service is [{needService}].");
1499+
$"[{makeServiceBindData.Service}]({makeServiceInstance.GetType()}) Attr inject type must be [{property.PropertyType}] , But instance is [{instance?.GetType()}] , Make service is [{needService}].");
15001500
}
15011501

15021502
property.SetValue(makeServiceInstance, instance, null);
@@ -1619,7 +1619,7 @@ protected virtual object[] GetDependencies(Bindable makeServiceBindData, Paramet
16191619
if (!CanInject(baseParam.ParameterType, param))
16201620
{
16211621
var error =
1622-
$"[{makeServiceBindData.Service}] Params inject type must be [{baseParam.ParameterType}] , But instance is [{param.GetType()}]";
1622+
$"[{makeServiceBindData.Service}] Params inject type must be [{baseParam.ParameterType}] , But instance is [{param?.GetType()}]";
16231623
if (needService == null)
16241624
{
16251625
error += " Inject params from user incoming parameters.";

0 commit comments

Comments
 (0)