Capturing a parameter with a Lambda - c#

What code is generated by the C# compiler when I try to capture the parameter of a function?
partial class NewClass : Window
{
public NewClass()
{
InitializeComponent();
new Thread(Work).Start();
}
void Work()
{
Thread.Sleep(5000); // Simulate time-consuming task
UpdateMessage("The answer");
}
void UpdateMessage(string message)
{
Action action = () => txtMessage.Text = message;
Dispatcher.BeginInvoke(action);
}
}
I know lambdas can hold variables in their lexical scope after a new class is created - where said variables are stored as fields. The fields replace any original occurrence of captured ones. But in this case, since no original value is being replaced, will a value be created from scratch? What's the magic behind this?

So what's going on here is that you're not actually closing over txtMessage, technically. What you're doing is closing over this.
Every instance method has an implicit first parameter of the type of the class itself, called this. The access of any instance members assumes that they are accessing members of that implicitly defined this variable.
Once you make all of that implicit code explicit, it becomes just another closure over any other locally scoped variable.
So first we make this explicit: (This isn't technically valid C#, it is merely for demonstrative purposes and is designed to be a representation of the semantic representation of the code by the C# compiler.)
void UpdateMessage(NewClass this, string message)
{
Action action = () => this.txtMessage.Text = message;
Dispatcher.BeginInvoke(action);
}
Then we do the traditional closure transformations of a locally scoped variable on that:
class ClosureClass1
{
public string message;
public NewClass #this;
public void AnonymousMethod1()
{
#this.txtMessage.Text = message;
}
}
void UpdateMessage(NewClass this, string message)
{
ClosureClass1 closure = new ClosureClass1();
closure.#this = this;
closure.message = message;
Action action = closure.AnonymousMethod1;
Dispatcher.BeginInvoke(action);
}

The NewClass contains a created private field of type <>c__DisplayClass1 class which contains a public string field 'message'. Each time the UpdateMessage is called the method instantiates a new instance of the <>c__DisplayClass1 class and assigns the message passed into the UpdateMessage method (by value). It also contains the < UpdateMessage >b__0 which is the action to execute (assign textbox with the message value). The textbox value is set by using the 'this' field which is the MainWindow class.
Hope this answers your question!

Related

Generically get the name of the current class?

When logging data, I want a generic reference to the containing class. That way, if the code is moved elsewhere, the class name will change accordingly. (Otherwise, if the code moves to nameof(Class2), it will still be logged incorrectly as nameof(Class1)). For example:
class Class_Name {
ICommand Command_Name =>
new RelayCommand(() =>
{
// An loggable event occurs
// Is there a smart and uncomplicated way of doing this generically?
var provenance = $"{nameof(Class_Name)}.{nameof(Command_Name)}";
// The event of whatever kind gets logged
});
}
// OR
void Method_Name() {
var provenance = $"{nameof(Class_Name)}.{nameof(Method_Name)}";
}
}
Using a generic nameof(this), where this should refer to the class itself, causes a compilation error: CS8081: Expression does not have a name. Using this.GetType() causes the same problem.
Not really understanding why the this keyword does not refer to the containing class in this context. Is there a way to refer to the current class generically?
If you combine the suggestion in the comments (this.GetType().Name) with a [CallerMemberName] attribute via a helper method, you can accomplish what you're looking for in a reusable fashion.
public class Class_Name
{
public void Method_Name()
{
var provenance = CreateProvenance();
Console.WriteLine(provenance);
}
private string CreateProvenance([CallerMemberName] string methodName = "")
{
return $"{this.GetType().Name}.{methodName}";
}
}
This outputs "Class_Name.Method_Name".
You can even turn this into a handy extension method that allows you to call it from any method.
public class Class_Name
{
public void Method_Name()
{
var provenance = this.CreateProvenance();
Console.WriteLine(provenance);
}
}
public static class ProvenanceExtensions
{
public static string CreateProvenance(this object context,
[CallerMemberName] string methodName = "")
{
return $"{context.GetType().Name}.{methodName}";
}
}
As Jeppe Stig Nielsen pointed out, you may not want the inheriting runtime type to be used, which is what context.GetType().Name will return. If you want to get the compile-time type instead, you can use generics.
public static class ProvenanceExtensions
{
public static string CreateProvenance<T>(this T context,
[CallerMemberName] string methodName = "")
{
return $"{typeof(T).Name}.{methodName}";
}
}
By design:
A nameof expression is evaluated at compile time and has no effect at
run time.
To access the type dynamically, in the runtime, you may use the GetType method. Just rememeber not to combine it with the nameof.
class Class_Name {
void Method_Name() {
// An event occurs
// Is there a smart and uncomplicated way of doing this generically?
var provenance = $"{this.GetType().Name}.{MethodBase.GetCurrentMethod().Name}";
// The event of whatever kind gets logged
}
}

Why can't my declared non static field be accessed inside static method?

Hi I am new to C# and I am aware on a surface level that I cannot use non static fields inside static methods. But I have a situation that I am trying to understand conceptually.
Check out this code snippet:
class CIMConversionHelper
{
private static Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ProcessingEndTimeData _procEndTimeData = new ProcessingEndTimeData();
public static TDX2KlarfResult HandleConversion(TDXProcessItem item, string fileName)
{
TDX2KlarfResult result = new TDX2KlarfResult();
result.success = true;
XmlDocument doc = new XmlDocument();
try
{
doc.Load(fileName);
}
catch (Exception ex)
{
result.success = false;
Logger.Error(ex, "XML Parsing Error: ");
return result;
}
_procEndTimeData.ToolType = toolType;
_procEndTimeData.Lot = input.cimToolContext.LOT;
_procEndTimeData.WaferScribe = input.cimWaferContainer.waferContext.WAFER_SCRIBE;
_procEndTimeData.Processing_End_Time = input.cimToolContext.PROCESSING_END_TIME;
}
public static TDX2KlarfResult Convert(TDXProcessItem item, string fileName)
{
TDX2KlarfResult result = new TDX2KlarfResult();
result.success = true;
try
{
result = CIMConversionHelper.HandleConversion(item, fileName);
}
catch (Exception ex)
{
// Failed to Parse the xml Not good mark nonrecoverable error and return.
result.errorType = "Non-Recoverable";
result.success = false;
Logger.Error(ex, "Unknown Error: ");
return result;
}
if (result.success)
{
//DBHelper.AddProcessingEndTimeToDB();
}
return result;
}
}
This is a very abridged snippet but one that captures my question. I have created an object reference as field for ProcessingEndTimeData called _procEndTimeData.
Why then is it telling me in Visual Studio that:
"an object reference is required for the non-static field, method, or property CIMConversionHelper._procEndTimeData?
I thought I should be able to assign values to the declared object "_procEndTimeData" in the 4th line inside the static function "HandleConversion"
Can someone explain to me why this reference is not enough? and why I would then have to create yet another ProcessingEndTimeData object inside the static function HandleCOnversion?
I know I can just toggle _procEndTimeData to be static but why do I need to do this if I have already created a reference in the field level?
Think about the memory of the equipment.
When you create an object, memory is reserved for it. Imagine, that because of its properties (int, char...) it occupies 32 bytes. If you create 10 objects, you will have 320 bytes occupied in memory for your objects.
But when you use "static", those properties are created only once for the class. Not once for each object you create.
So, your 10 objects can access their own properties (int, char...) and also the static ones. But from a "static" method you cannot access the properties of an object of that class because you don't have the instance of it.
A very simple example: You have a User class with the Name property. You add a static variable of type integer: static int count. That variable will be used to record the number of users you create.
In the constructor of the user you can do count++ because the instances of the class can access to the properties of the class. But if you create a static method:
public static void DoSomething()
{
// You can show a message with the number of users
MessageBox.Show(count.ToString());
// But you CAN'T access to a "Name" because this method is not
// a method of one concrete user. It's a general method, for all users
}
If you invoke DoSomething and you are created two users, "Tom" and "Jerry", in DoSomething you don't know any user.
You have 2 choices to make this work. Not sure which one applies. My guess is the first one I will show
private readonly ProcessingEndTimeData _procEndTimeData = new ProcessingEndTimeData();
change to
static private readonly ProcessingEndTimeData _procEndTimeData = new ProcessingEndTimeData();
This says that this field is also static, ie belongs to the class, not to instances of the CIMConversionHelper class.
The alternative, which I dont think you want it to create an instance of CIMConversionHelper in that method. Which as I said is probably not what you want.
If your intent is that this class (CIMConversionHelper) is all static, ie you will never create an instance of it then mark the class itself static and the compiler will ensure that you dont accidentally create non static members. ie
static class CIMConversionHelper{....}
Why is this so?
You say you have created a reference here
private readonly ProcessingEndTimeData _procEndTimeData = new ProcessingEndTimeData();
YOu have not created it yet.
You need to understand the difference between static and instance functions and fields.
Lets have an example. We have a class Thingy. It includes a factory method that makes thingy instances and keeps a count of how many it has made
public class Thingy{
static s_thingyCount = 0;
string _froop;
public static CreateThingy(){
var thing = new Thingy();
......
s_thingyCount++;
thing._froop = "hello";
return thing;
}
public void Twang(int froop){
......
}
public int Oink(string pling){
......
_froop = pling;
}
}
we can go
var t1 = Thingy.CreateThingy();
t1.Oink("Ole");
The CreateThingy does not operate on instances of the class, it operates on the class itself. The count variable does not belong to an instance, it belongs to the class itself. Note that in the create method we have to say
thing._froop = "hello";
ie which objects _froop we want to set (the one we are in the process of making).
var t1 = Thingy.CreateThingy();
now we have an instance of Thingy we can call methods on it
t1.Oink("Ole");
Look in that method
public int Oink(string pling){
......
_froop = pling;
}
we dont say which froop to set, we are manipulating an instance of the class.
We cannot do
Thingy.Oink("xxx");
Which THingy would be updates? Nor can we do
Thingy._froop = "fff";
for the same reason
but we can do
var count = Thingy.s_thingyCount;
How does this map to your class. THis method is static. It is like CreateThingy, it does not have an instance to operate on.
public static TDX2KlarfResult HandleConversion(TDXProcessItem item, string fileName)
but you do this
_procEndTimeData.ToolType = toolType;
with this field
private readonly ProcessingEndTimeData _procEndTimeData = new ProcessingEndTimeData();
this is just like doing
_froop = "hello"
in CreateThingy
_proceEndTimeData only exists in instances on your class.
It will be create when you do
new CIMConversionHelper();
But I suspect thats not what youo want to do. So you need to make _proceEndTimeData static, just like s_thingyCount
If I'm understanding it correctly, you answered yourself: "I am aware on a surface level that I cannot use non static fields inside static methods", and then you have declared a non static variable inside your method:
private readonly ProcessingEndTimeData _procEndTimeData
It should be static, apart than readonly if you want.
The reason for this has to do with object oriented programming. To access a static method from a class you don't need to instantiate it. This is why you can't reference class-level non static variables inside a static method from that class. Hope that made it a little clearer.
I know I can just toggle _procEndTimeData to be static but why do I need to do this if I have already created a reference in the field level?
You created no such thing, your _procEndTimeData is a field in every instance of CIMConversionHelper, of which you have none.
If it helps you visualize the problem better, imagine what would happen if your expectation was reality and you had the following code:
CIMConversionHelper h1 = new(), h2 = new();
CIMConversionHelper.Convert(.....whatever....);
Would it change h1._procEndTimeData? h2._procEndTimeData? It has to pick one, right? So which one does it pick?
No. static methods can only use static fields, period.

How can I add a default constructor and have it call another constructor and use the default values?

I have this code:
public class NewFrame
{
public NewFrame(string iconSource = Const.Car,
string iconColor = Const.Red)
{
When I try and use it then it's telling me I am missing a default constructor. How can I add one of these and still make the code use the default values for iconBackgroundColor and IconSource? I thought that adding in those defaults with the = Const. would make it work but it seems like it doesn't think my constructor is a default (with no params).
You just have to add another empty overload and call the required constructor with defaults. See below:
public class NewFrame
{
public NewFrame() : this(Const.Car, Const.Red){
}
public NewFrame(string iconSource,
string iconColor)
{
...
}
}
By having two optional parameters, you don't actually create 4 different constructor declarations under the hood (one with both parameters, one with the first parameter, one with the second parameter, and one with neither). There is still only one constructor, with two parameters. It's just that C# recognises that the parameters are optional, and has syntactic sugar to let you omit them when you call the constructor.
However, if you use reflection to create an instance of your class (probably whatever the thing that requires a default constructor is doing), and you attempt to invoke the parameterless constructor, it won't find one, because there is no syntactic sugar in reflection.
Here is an example:
class MainClass
{
public static void Main(string[] args)
{
Type t = typeof(MainClass);
object o = Activator.CreateInstance(t, 1);
Console.WriteLine(o);
}
public MainClass(int a = 10)
{
}
}
If you use typeof(MainClass).GetConstructors(), it will tell you that there is only one.
To actually declare a default constructor, you can do:
public class NewFrame
{
public NewFrame(string iconSource = Const.Car,
string iconColor = Const.Red)
{
...
}
public NewFrame() : this(Const.Car, Const.Red) { }
}
For what it's worth, when I do something like this, I take the route that #VyacheslavBenedichuk's answer is showing.
I'm not sure what your complaint is. This code compiles for me:
public class TestConstructor
{
public TestConstructor(string what = Const.Car, string color = Const.Red)
{
}
public static void Test()
{
var tc = new TestConstructor();
}
public class Const
{
public const string Car = "car";
public const string Red = "red";
}
}
What do your definitions of Const.Car and Const.Red look like? Where are you seeing the error?
But, if you use something that requires a default constructor, then this will not work. For example, this will fail at runtime:
var tc2 = Activator.CreateInstance(typeof(TestConstructor));
Please, when you are reporting an error, describe it exactly - in particular say whether it's a runtime or a compile-time error, the exact wording of the error, and the context in which the error occurs. In this case (the call to CreateInstance) will result in a System.MissingMethodException: 'No parameterless constructor defined for this object.'
In this case, you need to follow #VyacheslavBenedichuk's advice

Passing polymorphic types between viewmodels in mvvmcross

I have 2 buttons, that both are to show a new viewmodel, but with different data passed to the viewmodel. The data is passed is not completely different, so they both inherit from the same base. Unfortunately, it seems like the data passed to the Init() method of the viewmodel loses its runtime type information on the way? Using a debugger (or code), the passed data is only recognised as the base-class - NOT the derived class that was actually constructed and passed to the ShowViewModel<>() method.
Is what im trying to do not possible?
public class BasePasser
{
}
public class PasserA : BasePasser
{
}
public class PasserB : BasePasser
{
}
public class ViewModelOne : MvxViewModel
{
// ...
private void DoSwitchViewModelA()
{
var tobepassed = new PasserA {
// ...
};
ShowViewModel<ViewModelTwo>(tobepassed);
}
private void DoSwitchViewModelB()
{
var tobepassed = new PasserB {
// ...
};
ShowViewModel<ViewModelTwo>(tobepassed);
}
}
public class ViewModelTwo : MvxViewModel
{
// ...
public async Task Init(BasePasser passed)
{
if(passed is PasserA)
{
// ...
}
else if(passed is PasserB)
{
// ...
}
else
{
// Always called/hit
throw new InvalidOperationException("Unknown data passed");
}
}
}
Showing a ViewModel in MvvmCross is actually a pretty complicated process.
When you call ShowViewModel(param) Mvx will serialize the param value into a name/value dictionary. At this point, the Type information for param is lost.
When the ViewModel is instantiated, it will eventually look for any Init methods defined on the ViewModel.
If you look in Cirrious.MvvmCross.ViewModels.MvxViewModelExtensions.CallBundleMethod() you'll see that it first looks for a method with a single parameter of type IMvxBundle. Then it looks for a single parameter that is not a simple type (like string, int, etc.)
If it finds this type, it assumes it's an object that has been serialized in the ShowViewModel() call. It then attempts to deserialize the value based on the Type of the parameter in the Init method, NOT the original type.
This is why you only see the base type, since Mvx doesn't have the type information for the initial call, and when deserializing, only sees the base type from the Init method.
The workaround is to just put all properties into a single class. Only set the ones that you need and create a discriminator property that specifies which "type" it is.
var param = new Passer { PasserType = "A", ParamA = "ValueA", ... etc. }
You also have the option of using named method arguments.
See the Init section from https://github.com/MvvmCross/MvvmCross/wiki/View-Model-Lifecycle

C# Constructing immutable objects with delegates

I am trying to design a factory class that will make it easy for people on my team to add new "actions", I want the properties of the class to be immutable but for debugging purposes I the delegates need to be able to reference properties on the containing class.
Consider the following class:
public class NamedAction
{
public NamedAction(string name, Action action)
{
// Here there is some additional validation,
// such as null parameter checking, etc.
Name = name;
Action = action;
}
public string Name { get; private set; }
public Action Action { get; private set; }
public override string ToString()
{
return "Action: " + Name;
}
}
In a factory class I have the following method to create an instance of a special named action:
private static NamedAction GetSpecialAction()
{
NamedAction na = null;
na = new NamedAction(
"SpecialAction",
() => {
Console.WriteLine("My name is " + na.Name + " and I am special");
Console.WriteLine("Hi, its " + na.Name + " again. I like kittens.");
}
);
return na;
}
The problem I have with this is that ReSharper is telling that the second Console.WriteLine is accessing a modified closure. I guess its because the captured variable is actually still "null" when its reference is captured in the construction of the delegate because it is a parameter to the constructor.
To enforce correct usage I want to guarantee that the Action is not null and is immutable, and so far I haven't found any way to do it without a constructor. I thought about using a builder pattern but I don't see how you could provide access to "self" (this) in side the assigned actions, I'm wary about telling my team to ignore the modified closure warning.
I have an idea where I people could create blueprint/objects which are then sent to a builder/assembler class returning a concrete object but that really doesn't solve my problem of guaranteeing non-null at compile time.
Before giving up I was hoping there was some elegant solution to my problem...

Categories