无法显式调用运算符或访问器 无法对属性或索引器赋值

困扰了好久了。c#反汇编回来的程序报了2个错误,

 this.msc.add_Error(new DScriptControlSource_ErrorEventHandler(OnError));
 this.msc.add_Timeout(new DScriptControlSource_TimeoutEventHandler(OnTimeout));

提示:“MSScriptControl.DScriptControlSource_Event.Error.add”: 无法显式调用运算符或访问器

改成这样:

 this.msc.Error += new DScriptControlSource_ErrorEventHandler(OnError);

也一样不行,提示:无法对属性或索引器“MSScriptControl.IScriptControl.Error”赋值 -- 它是只读的

完整代码如下

 using MSScriptControl;
    using System;
    using System.Runtime.InteropServices;
    using System.Threading;

    public class ScriptEngine : IDisposable
    {
        public ScriptControl msc;

        public event RunErrorHandler RunError;

        public event RunTimeoutHandler RunTimeout;

        public ScriptEngine() : this(ScriptLanguage.VBscript)
        {
        }

        public ScriptEngine(ScriptLanguage language)
        {
            this.msc = new ScriptControlClass();
            this.msc.UseSafeSubset = true;
            this.msc.Language = language.ToString();
            this.msc.add_Error(new DScriptControlSource_ErrorEventHandler(OnError));
            this.msc.add_Timeout(new DScriptControlSource_TimeoutEventHandler(OnTimeout));

        }

        public void Dispose()
        {
            Marshal.ReleaseComObject(this.msc);
        }

        public object Eval(string expression, string codeBody)
        {
            this.msc.AddCode(codeBody);
            return this.msc.Eval(expression);
        }

        public object Eval(ScriptLanguage language, string expression, string codeBody)
        {
            if (this.Language != language)
            {
                this.Language = language;
            }
            return this.Eval(expression, codeBody);
        }

        private void OnError()
        {
            if (this.RunError != null)
            {
                this.RunError();
            }
        }

        private void OnTimeout()
        {
            if (this.RunTimeout != null)
            {
                this.RunTimeout();
            }
        }

        public void Reset()
        {
            this.msc.Reset();
        }

        public object Run(string mainFunctionName, object[] parameters, string codeBody)
        {
            this.msc.AddCode(codeBody);
            return this.msc.Run(mainFunctionName, ref parameters);
        }

        public object Run(ScriptLanguage language, string mainFunctionName, object[] parameters, string codeBody)
        {
            if (this.Language != language)
            {
                this.Language = language;
            }
            return this.Run(mainFunctionName, parameters, codeBody);
        }

        public bool AllowUI
        {
            get
            {
                return this.msc.AllowUI;
            }
            set
            {
                this.msc.AllowUI = value;
            }
        }

        public ScriptLanguage Language
        {
            get
            {
                return (ScriptLanguage) Enum.Parse(typeof(ScriptLanguage), this.msc.Language, false);
            }
            set
            {
                this.msc.Language = value.ToString();
            }
        }

        public int Timeout
        {
            get
            {
                return 0;
            }
        }

        public bool UseSafeSubset
        {
            get
            {
                return this.msc.UseSafeSubset;
            }
            set
            {
                this.msc.UseSafeSubset = true;
            }
        }
    }

查看全部
hui329337656
小小辉少
2015/08/23 14:25
  • c#
  • 属性
  • 索引器
  • 反汇编
  • 访问器
  • 点赞
  • 收藏
  • 回答
    私信

1个回复