Skip to content

Commit 1038d00

Browse files
committed
spelling correction
1 parent a74a955 commit 1038d00

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

README.md

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ SourceReflection aims to provide a more universal solution, offering `AOTable` R
3131
- Generic type definition
3232
- Generic method *
3333

34-
> Currently, support for generic type definition is not yet available. The main issue lies in the handling of `MarkGenericType`. I am currently experimenting with more effective approaches, but there is no specific plan at the moment.
34+
> Currently, support for generic type definition is not yet available. The main issue lies in the handling of `MakeGenericType`.
3535
36-
> Similar to generic types, there are also generic methods. The issue lies in `MarkGenericMethod`. However, it can currently handle some specific cases, such as situations where the generic type can be inferred.
36+
> Similar to generic types, there are also generic methods. The issue lies in `MakeGenericMethod`. However, it can currently handle some specific cases, such as situations where the generic type can be inferred.
37+
38+
`.NET 9` has made significant improvements to AOT and can support `MakeGenericType` and `MakeGenericMethod`. However, there is still a significant amount of work to be done in SourceReflection. Support will be added in subsequent versions
3739

3840
**Adapters**
3941
- `System.Text.Json` Adapter, supports `AOT` without JsonSerializerContext
@@ -125,13 +127,13 @@ Assert.AreEqual(1, type.DeclaredFields[1].GetValue(null));
125127

126128
## Array
127129

128-
The usage of `MarkArrayType` is similar to that of Runtime reflection.
130+
The usage of `MakeArrayType` is similar to that of Runtime reflection.
129131

130132
```c#
131133
[assembly: SourceReflectionType(typeof(int))]
132134

133135
SourceTypeInfo type = SourceReflector.GetType<int>();
134-
SourceTypeInfo arrayType = type.MarkArrayType();
136+
SourceTypeInfo arrayType = type.MakeArrayType();
135137

136138
int[] array = [1, 2];
137139
Assert.AreEqual(2, arrayType.GetRequriedProperty("Length").GetValue(array));
@@ -235,7 +237,7 @@ var o6 = SourceReflector.CreateInstance(typeof(CreateInstanceTestObject), 1, 2,
235237

236238
## Generic Definition
237239

238-
Currently, support for generic type definition is not yet available. The main issue lies in the handling of `MarkGenericType`. I am currently experimenting with more effective approaches, but there is no specific plan at the moment.
240+
Currently, support for generic type definition is not yet available. The main issue lies in the handling of `MakeGenericType`. I am currently experimenting with more effective approaches, but there is no specific plan at the moment.
239241

240242
```c#
241243
[assembly: SourceReflectionType(typeof(List<>))]
@@ -252,7 +254,7 @@ Assert.IsNull(SourceReflector.GetType<GenericTypeDefinitionTestObject<>>());
252254

253255
## Generic Type
254256

255-
Currently, support for generic type definition is not yet available. The main issue lies in the handling of `MarkGenericType`. The source generation can handle handle known types.
257+
Currently, support for generic type definition is not yet available. The main issue lies in the handling of `MakeGenericType`. The source generation can handle handle known types.
256258

257259
```c#
258260
[assembly: SourceReflectionType(typeof(List<string>))]
@@ -265,19 +267,30 @@ Assert.AreEqual(3, type.GetProperty("Count")!.GetValue(list));
265267

266268
## Generic Method
267269

268-
For generic methods with inferable types, they can be called using source generation
270+
If the parameter type can be inferred and cast to the constraint type, they can be called using source generation
269271

270272
```c#
271273
[SourceReflection]
272274
public class GenericMethodTestObject
273275
{
276+
//can't work (can not infer type parameter)
274277
public T Invoke0<T>() => default!;
278+
275279
public T Invoke1<T>(T t) => t;
276280
public T Invoke2<T>(T t) where T : ICloneable => t;
277-
public T Invoke3<T>(T t) where T : unmanaged => t;
278-
public T Invoke4<T>(T t) where T : notnull => t;
279-
public T Invoke5<T>(T t) where T : ICloneable, IComparable => t;
280-
public T Invoke6<T, K>(T t, K k) where T : ICloneable where K : IComparable => t;
281+
public T Invoke3<T>(T t) where T : notnull => t;
282+
public T Invoke4<T>(T t) where T : Enum => t;
283+
284+
//can't work (Unable to infer type. unable to cast object to unmanaged object)
285+
public T Invoke5<T>(T t) where T : unmanaged => t;
286+
287+
//can't work (Unable to infer type. unable to cast object to struct)
288+
public T Invoke6<T>(T t) where T : struct => t;
289+
290+
public T Invoke7<T>(T t) where T : ICloneable, IComparable => t;
291+
public T Invoke8<T, K>(T t, K k) where T : ICloneable where K : IComparable => t;
292+
293+
//can't work (Unable to infer type. unable to cast object to T[])
281294
public T[] InvokeArray1<T>(T[] t) => t;
282295
}
283296
```
@@ -289,23 +302,25 @@ GenericMethodTestObject instance = new();
289302
// Success
290303
type.GetMethod("Invoke1").Invoke(instance, [1]);
291304
type.GetMethod("Invoke2").Invoke(instance, [1]);
292-
type.GetMethod("Invoke4").Invoke(instance, [1]);
293-
type.GetMethod("Invoke5").Invoke(instance, [1]);
294-
type.GetMethod("Invoke6").Invoke(instance, [1, 2]);
305+
type.GetMethod("Invoke3").Invoke(instance, [1]);
306+
type.GetMethod("Invoke4").Invoke(instance, [Gender.Male]);
307+
type.GetMethod("Invoke7").Invoke(instance, [1]);
308+
type.GetMethod("Invoke8").Invoke(instance, [1, 2]);
295309

296-
//Error
310+
//Error (can not infer type parameter)
297311
type.GetMethod("Invoke0").Invoke(instance, []);
298-
type.GetMethod("Invoke3").Invoke(instance, [1]);
312+
type.GetMethod("Invoke5").Invoke(instance, [1]);
313+
type.GetMethod("Invoke6").Invoke(instance, [1]);
299314
type.GetMethod("InvokeArray1").Invoke(instance, [new int[] { 1 }]);
300315
```
301316

302-
When the generic type cannot be inferred, the only option is to use runtime reflection through `MarkGenericMethod`, which will not be supported in AOT compilation.
317+
When the parameter type can be inferred and cast to the constraint type, the only option is to use runtime reflection through `MakeGenericMethod`, which will not be supported in AOT compilation.
303318

304319
```c#
305-
type.GetMethod("Invoke0").MethodInfo.MarkGenericMethod([]).Invoke(instance);
320+
type.GetMethod("Invoke0").MethodInfo.MakeGenericMethod([]).Invoke(instance);
306321
```
307322

308-
Even if the type can be inferred, this approach has its drawbacks. If the internal implementation of the method has type checks on the generic parameters, the result may not meet expectations.
323+
Even if the type can be inferred and can be explicitly cast to a constrained type, this approach has its drawbacks. If the internal implementation of the method has type checks on the generic parameters, the result may not meet expectations.
309324

310325
```c#
311326
public class GenericMethodTestObject

src/SourceGeneration.Reflection.SourceGenerator/Models/SourceTypeParameterInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ internal class SourceTypeParameterInfo
66

77
public bool HasUnmanagedTypeConstraint;
88
public bool HasValueTypeConstraint;
9-
109
public bool HasTypeParameterInConstraintTypes;
1110
public string[] ConstraintTypes;
1211
}

src/SourceGeneration.Reflection.Test/ArrayTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class ArrayTest
77
public void Test()
88
{
99
var type = SourceReflector.GetRequiredType<int>();
10-
var arrayType = type.MarkArrayType();
10+
var arrayType = type.MakeArrayType();
1111

1212
int[] array = [1, 2];
1313
Assert.AreEqual(2, arrayType.GetRequriedProperty("Length").GetValue(array));

src/SourceGeneration.Reflection/SourceGeneration.Reflection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<LangVersion>12</LangVersion>
88
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
99

10-
<Version>1.0.0-beta2.250411.1</Version>
10+
<Version>1.0.0-beta2.250412.1</Version>
1111

1212
<Authors>SourceGeneration</Authors>
1313
<Description>Reflection based on Source Generator supports AOT compilation</Description>

0 commit comments

Comments
 (0)