Firstadvice I want to give you is: break down your problem. I see 3 distinct blocks of that code you want to form , 2 of which I'm sure you can manage yourself. When you have these composite questions, it feels like you're trying to get your homework done and the way you formed your question doesn't make it clear which part you are having troubles with.
I assume what you're trying to do to get the SE is get initial letters of each word in a cell. I haven't found a native excel way to do it. All of the sources I found require you to write a function of your own and this article is one of them and it explains the whole process pretty clearly.
This article provides an overview of Source Generators that ships as part of the .NET Compiler Platform ("Roslyn") SDK. Source Generators let C# developers inspect user code as it is being compiled. The generator can create new C# source files on the fly that are added to the user's compilation. In this way, you have code that runs during compilation. It inspects your program to produce additional source files that are compiled together with the rest of your code.
Retrieve a compilation object that represents all user code that is being compiled. This object can be inspected, and you can write code that works with the syntax and semantic models for the code being compiled, just like with analyzers today.
Generate C# source files that can be added to a compilation object during compilation. In other words, you can provide additional source code as input to a compilation while the code is being compiled.
When combined, these two things are what make Source Generators so useful. You can inspect user code with all of the rich metadata that the compiler builds up during compilation. Your generator then emits C# code back into the same compilation that is based on the data you've analyzed. If you're familiar with Roslyn Analyzers, you can think of Source Generators as analyzers that can emit C# source code.
Runtime reflection is a powerful technology that was added to .NET a long time ago. There are countless scenarios for using it. A common scenario is to perform some analysis of user code when an app starts up and use that data to generate things.
With a Source Generator, the controller discovery phase of startup could instead happen at compile time. A generator can analyze your source code and emit the code it needs to "wire up" your app. Using source generators could result in some faster startup times, since an action happening at run time today could get pushed into compile time.
Source Generators can improve performance in ways that aren't limited to reflection at run time to discover types as well. Some scenarios involve calling the MSBuild C# task (called CSC) multiple times so they can inspect data from a compilation. As you might imagine, calling the compiler more than once affects the total time it takes to build your app. We're investigating how Source Generators can be used to obviate the need for juggling MSBuild tasks like this, since Source generators don't just offer some performance benefits, but also allows tools to operate at the right level of abstraction.
Another capability Source Generators can offer is obviating the use of some "stringly typed" APIs, such as how
ASP.NET Core routing between controllers and razor pages work. With a Source Generator, routing can be strongly typed with the necessary strings being generated as a compile-time detail. This would reduce the number of times a mistyped string literal leads to a request not hitting the correct controller.
Replace the Program class with the following code. The following code doesn't use top level statements. The classic form is required because this first source generator writes a partial method in that Program class:
From the context object we can access the compilations' entry point, or Main method. The mainMethod instance is an IMethodSymbol, and it represents a method or method-like symbol (including constructor, destructor, operator, or property/event accessor). The Microsoft.CodeAnalysis.Compilation.GetEntryPoint method returns the IMethodSymbol for the program's entry point. Other methods enable you to find any method symbol in a project. From this object, we can reason about the containing namespace (if one is present) and the type. The source in this example is an interpolated string that templates the source code to be generated, where the interpolated holes are filled with the containing namespace and type information. The source is added to the context with a hint name. For this example, the generator creates a new generated source file that contains an implementation of the partial method in the console application. You can write source generators to add any source you'd like.
The hintName parameter from the GeneratorExecutionContext.AddSource method can be any unique name. It's common to provide an explicit C# file extension such as ".g.cs" or ".generated.cs" for the name. The file name helps identify the file as being source generated.
We now have a functioning generator, but need to connect it to our console application. Edit the original console application project and add the following, replacing the project path with the one from the .NET Standard project you created above:
This new reference isn't a traditional project reference, and has to be manually edited to include the OutputItemType and ReferenceOutputAssembly attributes. For more information on the OutputItemType and ReferenceOutputAssembly attributes of ProjectReference, see Common MSBuild project items: ProjectReference.
Now, when you run the console application, you should see that the generated code gets run and prints to the screen. The console application itself doesn't implement the HelloFrom method, instead it's source generated during compilation from the Source Generator project. The following text is an example output from the application:
You can also set build properties to save the generated file and control where the generated files are stored. In the console application's project file, add the element to a , and set its value to true. Build your project again. Now, the generated files are created under obj/Debug/net6.0/generated/SourceGenerator/SourceGenerator.HelloSourceGenerator. The components of the path map to the build configuration, target framework, source generator project name, and fully qualified type name of the generator. You can choose a more convenient output folder by adding the element to the application's project file.
The Source Generators Cookbook goes over some of these examples with some recommended approaches to solving them. Additionally, we have a set of samples available on GitHub that you can try on your own.
I've got an excel sheet that by default contains two comboboxes and their number is described by a variable x (x=2 by default). Each combobox is scripted to behave in a particular way in subs, for example I've got: private sub ComboBox1_DropButtonClick().
Nonetheless, sometimes I need to increase the number of these boxes by changing the value of X. I may need up to 10 comboboxes in total. Now the question is whether there's any way in which I can set the behaviour of an infinite number of comboboxes (for example in the event of DropButtonClick). What I did was to write a code for each of those comboboxes, so I've got a sub for ComboBox1_DropButtonClick(), ComboBox2_DropButtonClick(), ComboBox3_DropButtonClick(), etc.. The code varies a bit, but it's repeatable. So it all looks rather dumb and I'm searching for some more ingenious solution. Maybe all those comboboxes can be scripted in one go? If there's any way to do it, please share it with me.
I figured I might as well add the other approach for adding controls dynamically to an Excel sheet. I'd recommend sticking to UserForms, but, here's a method that should help out when controls are needed in a Sheet.
.NET 6 introduces the LoggerMessageAttribute type. This attribute is part of the Microsoft.Extensions.Logging namespace, and when used, it source-generates performant logging APIs. The source-generation logging support is designed to deliver a highly usable and highly performant logging solution for modern .NET applications. The auto-generated source code relies on the ILogger interface in conjunction with LoggerMessage.Define functionality.
The source generator is triggered when LoggerMessageAttribute is used on partial logging methods. When triggered, it is either able to autogenerate the implementation of the partial methods it's decorating, or produce compile-time diagnostics with hints about proper usage. The compile-time logging solution is typically considerably faster at run time than existing logging approaches. It achieves this by eliminating boxing, temporary allocations, and copies to the maximum extent possible.
In the preceding example, the logging method is static and the log level is specified in the attribute definition. When using the attribute in a static context, either the ILogger instance is required as a parameter, or modify the definition to use the this keyword to define the method as an extension method.
You may choose to use the attribute in a non-static context as well. Consider the following example where the logging method is declared as an instance method. In this context, the logging method gets the logger by accessing an ILogger field in the containing class.
Sometimes, the log level needs to be dynamic rather than statically built into the code. You can do this by omitting the log level from the attribute and instead requiring it as a parameter to the logging method.
As a general rule, the first instance of ILogger, LogLevel, and Exception are treated specially in the log method signature of the source generator. Subsequent instances are treated like normal parameters to the message template:
The generator does a case-insensitive comparison between items in the message template and argument names in the log message. This means that when the ILogger enumerates the state, the argument is picked up by the message template, which can make the logs nicer to consume:
3a8082e126