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

20,476

Solution 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);
);

Solution 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.

Solution 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
Share:
20,476
humblecoder
Author by

humblecoder

An aspirant of computers and a self learner. Passion to explore new technologies. Love to share knowledge with others.

Updated on July 12, 2022

Comments

  • humblecoder
    humblecoder almost 2 years

    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

  • humblecoder
    humblecoder over 13 years
    One more question? Then how can I add a factory method to the ExpandObject which would return my a dynamic student?
  • R. Martinho Fernandes
    R. Martinho Fernandes over 13 years
    @humblecoder: Do you mean this student.Get = new Func<dynamic>(() => student)?
  • humblecoder
    humblecoder over 13 years
    Hi Fernandes!I wanted to create dynamic object (an Active Record) which would have properties (read from the database table columns) and methods (dynamic FindByXXX) where XXX is the column name, similar to NHibernate finders. What's the best approach?
  • R. Martinho Fernandes
    R. Martinho Fernandes over 13 years
    @humblecoder: I guess that would warrant a whole other question.
  • humblecoder
    humblecoder over 13 years
    Assume there is a class called student: class Student { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } I want to dynamically add the following methods:FindById(),FindByFirstName() and FindByLastName() like this: dynamic student = typeof(Student); //dynamic student = new Student(); student.FindByFirstName = new Action(() => { Console.WriteLine("I am a lookup method and search the table by FirstName"); });
  • humblecoder
    humblecoder over 13 years
    and use in this way: dynamic martin = new Student(); martin.FirstName = "Martinho"; martin.LastName = "Fernandes"; martin.FindByFirstName();