yonan2236
yonan2236

Reputation: 13649

Build failed due to error - 'Value_Type._editor is a 'field' but is used like a 'type'

Can someone help me understand why I get this error in my code and how I can fix it?

'Value_Type._editor is a 'field' but is used like a 'type'

Please see my codes below:

    void MainFormLoad(object sender, EventArgs e)
    {                       
        Value_Type valueType = new Value_Type(typeof(TestEditorClass));
    }

My Value_Type class:

    public class Value_Type
    {
        object _editor;
        string _content;

        public Value_Type(object editor)
        {
            this._editor = editor;
            this._displayName = displayName;
        }

        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }

        [Editor(typeof(_editor), typeof(UITypeEditor))] // the error appears here when I pass _editor as a parameter to the attribute.
        public IRecord Key { get; set; }
    }

Upvotes: 0

Views: 34

Answers (2)

Adnan Umer
Adnan Umer

Reputation: 3689

There are few issues in your snippet

  1. typeof() requires a type and you are passing a variable that's why compiler is raising an error. To get type of object you can use object.GetType() instead. As you are storing a Type inside _editor so, simply cast to type (Type)_editor.

  2. _editor is a instance variable, you can't access that there. An object reference is required to access non-static member _editor.

Upvotes: 1

Charles Mager
Charles Mager

Reputation: 26213

typeof(_editor) isn't valid. As the error message tells you, the argument for typeof must be a type, e.g. typeof(TestEditorClass), not an instance of a type.

In this case, it looks like your _editor is a type, i.e (Type)_editor would be more appropriate than _editor.GetType().

However, neither of these on their own will resolve the issue as attribute arguments must be constants.

Upvotes: 2

Related Questions