public override bool ParseArgs(IScenarioContent content, ref VarArgs args, out string error) { // var a; // var b = 10; // var c = b; if (content.length != 2 && content.length != 4) { error = GetLengthErrorString(2, 4); return(false); } // 变量名只能包含数字,字母(中文),下划线,且不能以数字开头 if (!RegexUtility.IsMatchVariable(content[1])) { error = GetMatchVariableErrorString(content[1]); return(false); } args.name = content[1]; //// 你也可以使用这个方法,这里确保了变量必须不存在 //if (!IsMatchVar(content[1], false, ref args.name, out error)) //{ // return false; //} args.value = 0; if (content.length == 4) { if (content[2] != "=") { error = GetMatchOperatorErrorString(content[2], "="); return(false); } if (!ParseOrGetVarValue(content[3], ref args.value, out error)) { return(false); } } error = null; return(true); }
public Choice1(VarArgs varArgs, params object[] parameters) { I = 5; }
public static void TestActivatorCreateInstanceBinding() { Type t; object[] args; Choice1 c1; // Root the constructors if (string.Empty.Length > 0) { new Choice1(); new Choice1(123); new Choice1("Hey"); new Choice1(5.1); new Choice1(new VarArgs()); new Choice1(new VarStringArgs()); new Choice1(new VarIntArgs()); } t = null; args = new object[1]; Assert.Throws<ArgumentNullException>(() => Activator.CreateInstance(t, args)); t = typeof(Choice1); args = null; // Passing a null args is equivalent to passing an empty array of args. c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 1); t = typeof(Choice1); args = new object[] { }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 1); t = typeof(Choice1); args = new object[] { 42 }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 2); // Primitive widening is allowed by the binder. // but not by Dynamic.DelegateInvoke()... { t = typeof(Choice1); args = new object[] { (short)(-2) }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 2); } t = typeof(Choice1); args = new object[] { "Hello" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 3); t = typeof(Choice1); args = new object[] { null }; Assert.Throws<AmbiguousMatchException>(() => Activator.CreateInstance(t, args)); // "optional" parameters are not optional as far as Activator.CreateInstance() is concerned. t = typeof(Choice1); args = new object[] { 5.1 }; Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(t, args)); t = typeof(Choice1); args = new object[] { 5.1, Type.Missing }; Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(t, args)); t = typeof(Choice1); args = new object[] { 5.1, "Yes" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 4); // // "params" arguments are honored by Activator.CreateInstance() // VarArgs varArgs = new VarArgs(); t = typeof(Choice1); args = new object[] { varArgs }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 5); t = typeof(Choice1); args = new object[] { varArgs, "P1" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 5); t = typeof(Choice1); args = new object[] { varArgs, "P1", "P2" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 5); VarStringArgs varStringArgs = new VarStringArgs(); t = typeof(Choice1); args = new object[] { varStringArgs }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 6); t = typeof(Choice1); args = new object[] { varStringArgs, "P1" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 6); t = typeof(Choice1); args = new object[] { varStringArgs, "P1", "P2" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 6); t = typeof(Choice1); args = new object[] { varStringArgs, 5, 6 }; Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(t, args)); // // Primitive widening not supported for "params" arguments. // // (This is probably an accidental behavior on the desktop as the default binder specifically checks to see if the params arguments are widenable to the // params array element type and gives it the go-ahead if it is. Unfortunately, the binder then bollixes itself by using Array.Copy() to copy // the params arguments. Since Array.Copy() doesn't tolerate this sort of type mismatch, it throws an InvalidCastException which bubbles out // out of Activator.CreateInstance. Accidental or not, we'll inherit that behavior on .NET Native.) // VarIntArgs varIntArgs = new VarIntArgs(); t = typeof(Choice1); args = new object[] { varIntArgs, 1, (short)2 }; Assert.Throws<InvalidCastException>(() => Activator.CreateInstance(t, args)); return; }
public static void TestActivatorCreateInstanceBinding() { Type t; object[] args; Choice1 c1; // Root the constructors if (string.Empty.Length > 0) { new Choice1(); new Choice1(123); new Choice1("Hey"); new Choice1(5.1); new Choice1(new VarArgs()); new Choice1(new VarStringArgs()); new Choice1(new VarIntArgs()); } t = null; args = new object[1]; Assert.Throws <ArgumentNullException>(() => Activator.CreateInstance(t, args)); t = typeof(Choice1); args = null; // Passing a null args is equivalent to passing an empty array of args. c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 1); t = typeof(Choice1); args = new object[] { }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 1); t = typeof(Choice1); args = new object[] { 42 }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 2); // Primitive widening is allowed by the binder. // but not by Dynamic.DelegateInvoke()... { t = typeof(Choice1); args = new object[] { (short)(-2) }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 2); } t = typeof(Choice1); args = new object[] { "Hello" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 3); t = typeof(Choice1); args = new object[] { null }; Assert.Throws <AmbiguousMatchException>(() => Activator.CreateInstance(t, args)); // "optional" parameters are not optional as far as Activator.CreateInstance() is concerned. t = typeof(Choice1); args = new object[] { 5.1 }; Assert.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(t, args)); t = typeof(Choice1); args = new object[] { 5.1, Type.Missing }; Assert.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(t, args)); t = typeof(Choice1); args = new object[] { 5.1, "Yes" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 4); // // "params" arguments are honored by Activator.CreateInstance() // VarArgs varArgs = new VarArgs(); t = typeof(Choice1); args = new object[] { varArgs }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 5); t = typeof(Choice1); args = new object[] { varArgs, "P1" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 5); t = typeof(Choice1); args = new object[] { varArgs, "P1", "P2" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 5); VarStringArgs varStringArgs = new VarStringArgs(); t = typeof(Choice1); args = new object[] { varStringArgs }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 6); t = typeof(Choice1); args = new object[] { varStringArgs, "P1" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 6); t = typeof(Choice1); args = new object[] { varStringArgs, "P1", "P2" }; c1 = (Choice1)(Activator.CreateInstance(t, args)); Assert.Equal(c1.I, 6); t = typeof(Choice1); args = new object[] { varStringArgs, 5, 6 }; Assert.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(t, args)); // // Primitive widening not supported for "params" arguments. // // (This is probably an accidental behavior on the desktop as the default binder specifically checks to see if the params arguments are widenable to the // params array element type and gives it the go-ahead if it is. Unfortunately, the binder then bollixes itself by using Array.Copy() to copy // the params arguments. Since Array.Copy() doesn't tolerate this sort of type mismatch, it throws an InvalidCastException which bubbles out // out of Activator.CreateInstance. Accidental or not, we'll inherit that behavior on .NET Native.) // VarIntArgs varIntArgs = new VarIntArgs(); t = typeof(Choice1); args = new object[] { varIntArgs, 1, (short)2 }; Assert.Throws <InvalidCastException>(() => Activator.CreateInstance(t, args)); return; }
protected override ActionStatus Run(IGameAction gameAction, IScenarioContent content, VarArgs args, out string error) { ScenarioBlackboard.Set(args.name, args.value); error = null; return(ActionStatus.Continue); }