Skip to content

Commit 1c884f6

Browse files
committed
resolved #175
1 parent 41bc33c commit 1c884f6

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,43 @@ public int TestContainerCall(int num)
22492249
return num;
22502250
}
22512251

2252+
public class TestNullableValue
2253+
{
2254+
public int? Nullable1 { get; set; }
2255+
public byte? Nullable2 { get; set; }
2256+
2257+
[Inject]
2258+
public int? Nullable3 { get; set; }
2259+
2260+
[Inject]
2261+
public byte? Nullable4 { get; set; }
2262+
2263+
public TestNullableValue(int? nullable1, byte? nullable2)
2264+
{
2265+
Nullable1 = nullable1;
2266+
Nullable2 = nullable2;
2267+
}
2268+
}
2269+
2270+
[TestMethod]
2271+
public void TestNullable()
2272+
{
2273+
var container = new Container();
2274+
container.Singleton<TestNullableValue>().Needs("$Nullable3").Given(() => 100);
2275+
2276+
var v = container.Make<TestNullableValue>(10);
2277+
2278+
Assert.AreEqual(true, v.Nullable1.HasValue);
2279+
Assert.AreEqual(10, v.Nullable1.Value);
2280+
2281+
Assert.AreEqual(false, v.Nullable2.HasValue);
2282+
2283+
Assert.AreEqual(true, v.Nullable3.HasValue);
2284+
Assert.AreEqual(100, v.Nullable3.Value);
2285+
2286+
Assert.AreEqual(false, v.Nullable4.HasValue);
2287+
}
2288+
22522289
#region Rebound
22532290
[TestMethod]
22542291
public void TestOnRebound()

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,19 @@ protected virtual bool ResloveFromContextual(Bindable makeServiceBindData, strin
12401240
/// <returns>解决结果</returns>
12411241
protected virtual object ResolveAttrPrimitive(Bindable makeServiceBindData, string service, PropertyInfo baseParam)
12421242
{
1243-
return ResloveAttrClass(makeServiceBindData, service, baseParam);
1243+
if (ResloveFromContextual(makeServiceBindData, service, baseParam.Name, baseParam.PropertyType,
1244+
out object instance))
1245+
{
1246+
return instance;
1247+
}
1248+
1249+
if (baseParam.PropertyType.IsGenericType && baseParam.PropertyType.GetGenericTypeDefinition() ==
1250+
typeof(Nullable<>))
1251+
{
1252+
return null;
1253+
}
1254+
1255+
throw MakeUnresolvableException(baseParam.Name, baseParam.DeclaringType);
12441256
}
12451257

12461258
/// <summary>
@@ -1270,7 +1282,25 @@ protected virtual object ResloveAttrClass(Bindable makeServiceBindData, string s
12701282
/// <returns>解决结果</returns>
12711283
protected virtual object ResolvePrimitive(Bindable makeServiceBindData, string service, ParameterInfo baseParam)
12721284
{
1273-
return ResloveClass(makeServiceBindData, service, baseParam);
1285+
if (ResloveFromContextual(makeServiceBindData, service, baseParam.Name, baseParam.ParameterType,
1286+
out object instance))
1287+
{
1288+
return instance;
1289+
}
1290+
1291+
if (baseParam.IsOptional)
1292+
{
1293+
return baseParam.DefaultValue;
1294+
}
1295+
1296+
if (baseParam.ParameterType.IsGenericType && baseParam.ParameterType.GetGenericTypeDefinition() ==
1297+
typeof(Nullable<>))
1298+
{
1299+
return null;
1300+
}
1301+
1302+
throw MakeUnresolvableException(baseParam.Name,
1303+
baseParam.Member != null ? baseParam.Member.DeclaringType : null);
12741304
}
12751305

12761306
/// <summary>

0 commit comments

Comments
 (0)