示例#1
0
        protected virtual void LoadLanguageTarget(string language)
        {
            string targetName = "Antlr4.Codegen.Target." + language + "Target";

            try
            {
                Type c = Type.GetType(targetName, true);
                target = (AbstractTarget)Activator.CreateInstance(c, this);
            }
            catch (TargetInvocationException e)
            {
                tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR,
                                      e,
                                      targetName);
            }
            catch (TypeLoadException e)
            {
                tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR,
                                      e,
                                      targetName);
            }
            catch (ArgumentException e)
            {
                tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR,
                                      e,
                                      targetName);
            }
            catch (InvalidCastException e)
            {
                tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR,
                                      e,
                                      targetName);
            }
        }
示例#2
0
            internal OCAnonymousClassWrapper(SerializationInfo info, StreamingContext context)
            {
                Type classType = (Type)info.GetValue("classType", typeof(Type));

                obj = Activator.CreateInstance(classType);

                foreach (FieldInfo field in classType.GetFields())
                {
                    //If the field is a delegate
                    if (typeof(Delegate).IsAssignableFrom(field.FieldType))
                    {
                        field.SetValue(obj,
                                       ((OCDelegate)info.GetValue
                                            (field.Name, typeof(OCDelegate)))
                                       .Delegate);
                    }
                    //If the field is an anonymous class
                    else
                    if (!field.FieldType.IsSerializable)
                    {
                        field.SetValue(obj,
                                       ((OCAnonymousClassWrapper)info.GetValue
                                            (field.Name, typeof(OCAnonymousClassWrapper)))
                                       .obj);
                    }
                    //otherwise
                    else
                    {
                        field.SetValue(obj, info.GetValue(field.Name, field.FieldType));
                    }
                }
            }
示例#3
0
 public object newInstance()
 {
     try
     {
         return(Activator.CreateInstance(Type));
     }
     catch (SysException ex)
     {
         var cause = new Exception(ex.Message);
         throw new TypeException($"The type doesn't have a default constructor: {getName()}.", cause);
     }
 }
示例#4
0
 public object CreateInstance(Type type)
 {
     if (type == typeof(string))
     {
         return("");
     }
     if (type == typeof(void))
     {
         return(null);
     }
     return(Activator.CreateInstance(type));
 }
        public static T CreateByType <T>(Type type)
        {
            if (type == null)
            {
                //ExternalHelpers.logger.Send($"::ReflectionHelper.CreateByType:: Object Type == null");
                return(default(T));
            }

            //ExternalHelpers.logger.Send($"::ReflectionHelper.CreateByType:: Serialized object of type {type}");
            T obj = (T)Activator.CreateInstance(type);

            return(obj);
        }
示例#6
0
 public void SetKeys(ColorKey[] colorKeys, AlphaKey[] alphaKeys)
 {
     if (colorKeys != null)
     {
         Array colorKeyParam = (Array)Activator.CreateInstance(s_tyGradientColorKey.MakeArrayType(), new object[] { colorKeys.Length });
         for (int i = 0; i < colorKeys.Length; ++i)
         {
             colorKeyParam.SetValue(Activator.CreateInstance(s_tyGradientColorKey, colorKeys[i].color, colorKeys[i].time), i);
         }
         s_piColorKeys.SetValue(_gradient, colorKeyParam, null);
     }
     if (alphaKeys != null)
     {
         Array alphaKeyParam = (Array)Activator.CreateInstance(s_tyGradientAlphaKey.MakeArrayType(), new object[] { alphaKeys.Length });
         for (int i = 0; i < alphaKeys.Length; ++i)
         {
             alphaKeyParam.SetValue(Activator.CreateInstance(s_tyGradientAlphaKey, alphaKeys[i].alpha, alphaKeys[i].time), i);
         }
         s_piAlphaKeys.SetValue(_gradient, alphaKeyParam, null);
     }
 }
示例#7
0
        /** Derive a new parser from an old one that has knowledge of the grammar.
         *  The Grammar object is used to correctly compute outer alternative
         *  numbers for parse tree nodes. A parser of the same type is created
         *  for subclasses of {@link ParserInterpreter}.
         */
        public static ParserInterpreter DeriveTempParserInterpreter(Grammar g, Parser originalParser, ITokenStream tokens)
        {
            ParserInterpreter parser;

            if (originalParser is ParserInterpreter)
            {
                Type c = originalParser.GetType();
                try
                {
                    parser = (ParserInterpreter)Activator.CreateInstance(originalParser.GetType(), g, originalParser.Atn, originalParser.InputStream);
                }
                catch (Exception e)
                {
                    throw new ArgumentException("can't create parser to match incoming " + c.Name, e);
                }
            }
            else
            {
                // must've been a generated parser
                char[] serializedAtn = ATNSerializer.GetSerializedAsChars(originalParser.Atn, originalParser.RuleNames);
                ATN    deserialized  = new ATNDeserializer().Deserialize(serializedAtn);
                parser = new ParserInterpreter(originalParser.GrammarFileName,
                                               originalParser.Vocabulary,
                                               originalParser.RuleNames,
                                               deserialized,
                                               tokens);
            }

            parser.SetInputStream(tokens);

            // Make sure that we don't get any error messages from using this temporary parser
            parser.ErrorHandler = new BailErrorStrategy();
            parser.RemoveErrorListeners();
            parser.RemoveParseListeners();
            parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection;
            return(parser);
        }
 public object clone() => Activator.CreateInstance(typeof(T), this);