“Keyword 'this' is not valid in a static property, static method, or static field initializer” when adding methods to an ExpandoObject

给你一囗甜甜゛ 提交于 2021-02-07 01:27:00

问题


I am try to add a dynamic method to ExpandoObject which would return the properties (added dynamically) to it, however it's always giving me error.

Is something wrong I am doing here?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace DynamicDemo
{
class ExpandoFun
{
    public static void Main()
    {
        Console.WriteLine("Fun with Expandos...");
        dynamic student = new ExpandoObject();
        student.FirstName = "John";
        student.LastName = "Doe";
        student.Introduction=new Action(()=>
      Console.WriteLine("Hello my name is {0} {1}",this.FirstName,this.LastName);
    );

        Console.WriteLine(student.FirstName);
        student.Introduction();
    }
}
}

The Compiler is flagging the following error: Error 1

Keyword 'this' is not valid in a static property, static method, or static field initializer

D:\rnd\GettingStarted\DynamicDemo\ExpandoFun.cs 20 63 DynamicDemo


回答1:


Well, you're using this in the lambda, which would refer to the object that is creating the Action. You cannot do that because you're in a static method.

Even if you were in an instance method, it would not work with this because it would refer to the instance of the object creating the Action, not the ExpandoObject where you're tucking it.

You need to reference the ExpandoObject (student):

student.Introduction=new Action(()=>
    Console.WriteLine("Hello my name is {0} {1}",student.FirstName,student.LastName);
);



回答2:


There's no "this" available to you.

Capture the object when creating the lambda instead:

student.Introduction = new Action( ()=> Console.WriteLine("Hello my name is {0} {1}", student.FirstName, student.LastName) );

Then it works.




回答3:


You can't use this keyword in the action, because here this refers to the current instance(not student), which causes the compile error because the code is in a static method. Check this:

dynamic student = new ExpandoObject();
student.FirstName = "John";
student.LastName = "Doe";
student.Introduction = new Action(() => Console.WriteLine("Hello my name is {0} {1}", student.FirstName, student.LastName));
Console.WriteLine(student.FirstName);
student.Introduction();
student.FirstName = "changed";
Console.WriteLine(student.FirstName);
student.Introduction();

Output:

John Doe
Hello my name is John Doe
changed Doe
Hello my name is changed Doe



回答4:


You are invoking the action code from the static Main method. There you cannot access object properties. You must replace it with

student.Introduction = new Action(
    () =>
    {
        Console.WriteLine("Hello my name is {0} {1}", student.FirstName, student.LastName);
    };

e.g.



来源:https://stackoverflow.com/questions/4537141/keyword-this-is-not-valid-in-a-static-property-static-method-or-static-fiel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!