小小辉少 2015-08-23 14:25 采纳率: 50%
浏览 2675

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

困扰了好久了。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;
            }
        }
    }

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-08-23 14:37
    关注

    参考:
    http://blog.chdz1.com/NETjs/158.html

    重新引用下ScriptControl

       ((DScriptControlSource_Event)msc).Error += new DScriptControlSource_ErrorEventHandler(ScriptEngine_Error);
                ((DScriptControlSource_Event)msc).Timeout += new DScriptControlSource_TimeoutEventHandler(ScriptEngine_Timeout);
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码