C#中Alexa类实现求助:Lambda配置后Talk方法输出问题
Got it, let's get your Alexa class working properly! The core issue right now is that your Alexa class doesn't persist the configuration changes made via the Configure method, so the Talk method has no way to access those settings. Here's how to fix it step by step:
Step 1: Add a Persistent Configuration Field to Alexa
First, your Alexa class needs to hold onto an instance of Configration so that any changes made in Configure are saved for later use in Talk. We'll initialize this field with a new Configration object by default.
Step 2: Update the Configure Method
The Configure method should pass the class's internal configuration instance to the provided Action<Configration>, so the lambda you write actually modifies the settings Alexa will use.
Step 3: Implement the Talk Method
In Talk, we'll check if a custom greeting message was configured. If it was, we'll replace the {OwnerName} placeholder with the actual owner name; if not, we'll return the default greeting.
Full Working Code
public class Alexa { // Private field to store our configuration settings private readonly Configration _config = new Configration(); public string Talk() { // Use custom greeting if configured, otherwise default if (!string.IsNullOrEmpty(_config.GreetingMessage)) { // Replace the {OwnerName} placeholder with the actual owner name return _config.GreetingMessage.Replace("{OwnerName}", _config.OwnerName); } return "hello, i am Alexa"; } public void Configure(Action<Configration> configureAction) { // Apply the user's configuration changes to our internal config instance configureAction?.Invoke(_config); } } public class Configration { public string GreetingMessage { get; set; } public string OwnerName { get; set; } }
How It Works
- Persistent Configuration: The
_configfield keeps track of all settings set viaConfigure. This means changes aren't lost after theConfiguremethod finishes. - Dynamic Greeting: The
Talkmethod checks if a customGreetingMessageexists. If it does, it usesstring.Replaceto swap{OwnerName}with the value stored in_config.OwnerName. If no custom greeting is set, it falls back to the default message. - Configure Method: The
configureAction?.Invoke(_config)line ensures the lambda you pass (likex => { x.GreetingMessage = "..."; x.OwnerName = "Bob Marley"; }) modifies the internal configuration instance directly.
Testing the Code
When you run your original test code:
var alexa = new Alexa(); Console.WriteLine(alexa.Talk()); // Output: hello, i am Alexa alexa.Configure(x => { x.GreetingMessage = "Hello {OwnerName}, I am at your service"; x.OwnerName = "Bob Marley"; }); Console.WriteLine(alexa.Talk()); // Output: Hello Bob Marley, I am at your service
It will now produce the expected output!
Alternative: Using String.Format (If You Prefer Index Placeholders)
If you'd rather use string.Format instead of Replace, you can adjust the greeting template to use {0} instead of {OwnerName}:
// In your Configure call: x.GreetingMessage = "Hello {0}, I am at your service"; // And in the Talk method: return string.Format(_config.GreetingMessage, _config.OwnerName);
This works just as well—pick whichever approach fits your coding style.
内容的提问来源于stack exchange,提问作者Asif

