-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update method builder #352
Conversation
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe changes primarily involve the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant NatashaSlimMethodBuilder
participant AssemblyCSharpBuilder
participant NatashaLoadContext
User->>+NatashaSlimMethodBuilder: Create Instance with "script"
NatashaSlimMethodBuilder->>User: Return Builder Instance
User->>+NatashaSlimMethodBuilder: Configure using AssemblyCSharpBuilder
NatashaSlimMethodBuilder->>+AssemblyCSharpBuilder: Apply Configuration
AssemblyCSharpBuilder-->>-NatashaSlimMethodBuilder: Config Applied
User->>+NatashaSlimMethodBuilder: Configure using NatashaLoadContext
NatashaSlimMethodBuilder->>+NatashaLoadContext: Apply Configuration
NatashaLoadContext-->>-NatashaSlimMethodBuilder: Config Applied
User->>+NatashaSlimMethodBuilder: Generate Delegate
NatashaSlimMethodBuilder->>User: Delegate Returned
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
@coderabbitai review |
Actions performedReview triggered.
|
未检测到合适的 ISSUE 推荐给您。感谢您的反馈!
|
UT Test - Ubuntu1 tests 1 ✅ 0s ⏱️ Results for commit 3e5af70. |
UT Test - Windows1 tests 1 ✅ 0s ⏱️ Results for commit 3e5af70. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.MethodCreator/NatashaSlimMethodBuilder.cs (1 hunks)
- src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.MethodCreator/StringExtension.cs (1 hunks)
- src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.MethodCreator/SystemDelegateExtension.cs (1 hunks)
Additional comments not posted (14)
src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.MethodCreator/StringExtension.cs (3)
10-14
: LGTM!The method correctly initializes
NatashaSlimMethodBuilder
and configures it usingAssemblyCSharpBuilder
.
17-21
: LGTM!The method correctly initializes
NatashaSlimMethodBuilder
and configures it usingNatashaLoadContext
.
24-28
: LGTM!The method correctly initializes
NatashaSlimMethodBuilder
and configures it usingWithSimpleBuilder
.src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.MethodCreator/NatashaSlimMethodBuilder.cs (9)
15-20
: LGTM!The constructor correctly initializes
Usings
,Builder
, andScript
.
21-25
: LGTM!The method correctly sets
PrivateObjects
and returns the builder instance.
27-31
: LGTM!The method correctly sets
_builderConfig
and returns the builder instance.
32-36
: LGTM!The method correctly sets
_ctxConfig
and returns the builder instance.
37-43
: LGTM!The method correctly configures the builder using
UseRandomLoadContext
,UseSimpleMode
, andConfigLoadContext
.
45-49
: LGTM!The method correctly adds the provided usings to the
Usings
set.
51-55
: LGTM!The method correctly configures the builder to add reference and using code for the specified type.
116-119
: LGTM!The method correctly delegates to
ToDelegate<T>
with the "async" modifier.
120-127
: LGTM!The methods correctly delegate to
ToDelegate<T>
with the appropriate modifiers.src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.MethodCreator/SystemDelegateExtension.cs (2)
9-55
: LGTM!The methods correctly delegate to
ToDelegate<T>
for variousAction
types.
61-107
: LGTM!The methods correctly delegate to
ToDelegate<T>
for variousFunc
types.
public NatashaSlimMethodBuilder WithMetadata(params Type[] types) | ||
{ | ||
if (types!=null && types.Length == 0) | ||
{ | ||
foreach (var type in types) | ||
{ | ||
Builder.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode(type)); | ||
} | ||
} | ||
return this; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the condition check for empty types array.
The condition check for types.Length == 0
should be types.Length > 0
to avoid unnecessary iteration.
- if (types!=null && types.Length == 0)
+ if (types!=null && types.Length > 0)
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
public NatashaSlimMethodBuilder WithMetadata(params Type[] types) | |
{ | |
if (types!=null && types.Length == 0) | |
{ | |
foreach (var type in types) | |
{ | |
Builder.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode(type)); | |
} | |
} | |
return this; | |
} | |
public NatashaSlimMethodBuilder WithMetadata(params Type[] types) | |
{ | |
if (types!=null && types.Length > 0) | |
{ | |
foreach (var type in types) | |
{ | |
Builder.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode(type)); | |
} | |
} | |
return this; | |
} |
public T ToDelegate<T>(string modifier = "") where T : Delegate | ||
{ | ||
_builderConfig?.Invoke(Builder); | ||
_ctxConfig?.Invoke(Builder.LoadContext); | ||
|
||
var className = $"N{Guid.NewGuid():N}"; | ||
var methodInfo = typeof(T).GetMethod("Invoke")!; | ||
|
||
var returnTypeScript = methodInfo.ReturnType.GetDevelopName(); | ||
var parameterScript = new StringBuilder(); | ||
|
||
var methodParameters = methodInfo.GetParameters(); | ||
for (int i = 0; i < methodParameters.Length; i += 1) | ||
{ | ||
var paramType = methodParameters[i].ParameterType; | ||
Builder.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode(paramType)); | ||
parameterScript.Append($"{paramType.GetDevelopName()} arg{i + 1},"); | ||
} | ||
if (parameterScript.Length > 0) | ||
{ | ||
parameterScript.Length -= 1; | ||
} | ||
|
||
StringBuilder usingCode = new(); | ||
foreach (var item in Usings) | ||
{ | ||
|
||
usingCode.AppendLine($"using {item};"); | ||
|
||
} | ||
var fullScript = $"{usingCode} public static class {className} {{ public static {(modifier ?? string.Empty)} {returnTypeScript} Invoke({parameterScript}){{ {Script} }} }}"; | ||
if (PrivateObjects!=null) | ||
{ | ||
Builder.Add(fullScript.ToAccessPrivateTree(PrivateObjects)); | ||
} | ||
else | ||
{ | ||
Builder.Add(fullScript); | ||
} | ||
var asm = Builder.GetAssembly(); | ||
var type = asm.GetType(className); | ||
if (type != null) | ||
{ | ||
return (T)Delegate.CreateDelegate(typeof(T), type.GetMethod("Invoke")!); | ||
} | ||
throw new Exception($"未找到 {className} 类型!"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle potential null reference for methodInfo
.
The methodInfo
variable should be checked for null before accessing its properties.
- var methodInfo = typeof(T).GetMethod("Invoke")!;
+ var methodInfo = typeof(T).GetMethod("Invoke");
+ if (methodInfo == null)
+ {
+ throw new InvalidOperationException($"The delegate type {typeof(T)} does not have an Invoke method.");
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
public T ToDelegate<T>(string modifier = "") where T : Delegate | |
{ | |
_builderConfig?.Invoke(Builder); | |
_ctxConfig?.Invoke(Builder.LoadContext); | |
var className = $"N{Guid.NewGuid():N}"; | |
var methodInfo = typeof(T).GetMethod("Invoke")!; | |
var returnTypeScript = methodInfo.ReturnType.GetDevelopName(); | |
var parameterScript = new StringBuilder(); | |
var methodParameters = methodInfo.GetParameters(); | |
for (int i = 0; i < methodParameters.Length; i += 1) | |
{ | |
var paramType = methodParameters[i].ParameterType; | |
Builder.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode(paramType)); | |
parameterScript.Append($"{paramType.GetDevelopName()} arg{i + 1},"); | |
} | |
if (parameterScript.Length > 0) | |
{ | |
parameterScript.Length -= 1; | |
} | |
StringBuilder usingCode = new(); | |
foreach (var item in Usings) | |
{ | |
usingCode.AppendLine($"using {item};"); | |
} | |
var fullScript = $"{usingCode} public static class {className} {{ public static {(modifier ?? string.Empty)} {returnTypeScript} Invoke({parameterScript}){{ {Script} }} }}"; | |
if (PrivateObjects!=null) | |
{ | |
Builder.Add(fullScript.ToAccessPrivateTree(PrivateObjects)); | |
} | |
else | |
{ | |
Builder.Add(fullScript); | |
} | |
var asm = Builder.GetAssembly(); | |
var type = asm.GetType(className); | |
if (type != null) | |
{ | |
return (T)Delegate.CreateDelegate(typeof(T), type.GetMethod("Invoke")!); | |
} | |
throw new Exception($"未找到 {className} 类型!"); | |
} | |
public T ToDelegate<T>(string modifier = "") where T : Delegate | |
{ | |
_builderConfig?.Invoke(Builder); | |
_ctxConfig?.Invoke(Builder.LoadContext); | |
var className = $"N{Guid.NewGuid():N}"; | |
var methodInfo = typeof(T).GetMethod("Invoke"); | |
if (methodInfo == null) | |
{ | |
throw new InvalidOperationException($"The delegate type {typeof(T)} does not have an Invoke method."); | |
} | |
var returnTypeScript = methodInfo.ReturnType.GetDevelopName(); | |
var parameterScript = new StringBuilder(); | |
var methodParameters = methodInfo.GetParameters(); | |
for (int i = 0; i < methodParameters.Length; i += 1) | |
{ | |
var paramType = methodParameters[i].ParameterType; | |
Builder.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode(paramType)); | |
parameterScript.Append($"{paramType.GetDevelopName()} arg{i + 1},"); | |
} | |
if (parameterScript.Length > 0) | |
{ | |
parameterScript.Length -= 1; | |
} | |
StringBuilder usingCode = new(); | |
foreach (var item in Usings) | |
{ | |
usingCode.AppendLine($"using {item};"); | |
} | |
var fullScript = $"{usingCode} public static class {className} {{ public static {(modifier ?? string.Empty)} {returnTypeScript} Invoke({parameterScript}){{ {Script} }} }}"; | |
if (PrivateObjects!=null) | |
{ | |
Builder.Add(fullScript.ToAccessPrivateTree(PrivateObjects)); | |
} | |
else | |
{ | |
Builder.Add(fullScript); | |
} | |
var asm = Builder.GetAssembly(); | |
var type = asm.GetType(className); | |
if (type != null) | |
{ | |
return (T)Delegate.CreateDelegate(typeof(T), type.GetMethod("Invoke")!); | |
} | |
throw new Exception($"未找到 {className} 类型!"); | |
} |
Summary by CodeRabbit
New Features
WithSlimMethodBuilder
method inStringExtension
allowing configuration ofNatashaSlimMethodBuilder
instances for more streamlined method creation.WithSlimMethodBuilder
methods that accept configuration parameters for advanced customizations.Refactor
SystemDelegateExtension
methods to utilizeNatashaSlimMethodBuilder
for delegate generation, simplifying method signatures and improving consistency.Enhancements
NatashaSlimMethodBuilder
, offering more flexible usage patterns and configurations.