public void AddValue(string favName, string category, string description, string objectName, string propertyName)
        {
            string            objectNameForCode = "o" + objectCount++;
            IObjectDefinition defObj            = mTestSequence.ObjectRegistry.GetObject(objectName);

            if (defObj == null)
            {
                throw new ArgumentException("Can't find object '" + objectName + "'");
            }

            string objectType = defObj.GetType().ToString();

            objectType = objectType.Substring(0, objectType.Length - 10) + "Instance";
//            string propertyType = defObj.GetType().GetProperty(propertyName).PropertyType.ToString();

            // o1 = (GeneratedValueInstance)mTestExecution.GetObjectIfExists("IntensityVariation");
            //updateBody += "\t\t" + objectNameForCode + " = (" + objectType + ")mTestExecution.GetObjectIfExists(\"" + objectName + "\");" + Environment.NewLine;
            // o1 = null; if(mTestExecution!=null) o1 = (GeneratedValueInstance)mTestExecution.GetObjectIfExists("IntensityVariation");
            updateBody += "\t\t" + objectNameForCode + " = null; if(mTestExecution!=null) " + objectNameForCode + " = (" + objectType + ")mTestExecution.ObjectRegistry.GetObject(\"" + objectName + "\");" + Environment.NewLine;

            // private GeneratedValueInstance o1;
            classBody += "\tprivate " + objectType + " " + objectNameForCode + ";" + Environment.NewLine;

            // property attributes
            // [CategoryAttribute("Parameters"),DescriptionAttribute("Definition of the color to search for")]
            classBody += "\t[CategoryAttribute(\"" + category + "\"),DescriptionAttribute(\"" + description + "\")]" + Environment.NewLine;

            // create the property

            /*
             *  public string FavoriteTitle
             *  {
             *      get { return o1 == null ? "" : o1.Value; }
             *  }
             */
            classBody += "\tpublic string " + favName + Environment.NewLine;  // NOTE: casting everything to string so we can easilly handle o1==null...
            classBody += "\t{" + Environment.NewLine;
            classBody += "\t\tget { return " + objectNameForCode + " == null ? \"\" : " + objectNameForCode + "." + propertyName + "; }" + Environment.NewLine;
//            classBody += "\t\tset {}" + Environment.NewLine;
            classBody += "\t}" + Environment.NewLine;
        }
예제 #2
0
 public OwnerLink(IObjectDefinition theOwner, string theOwnersPropertyName)
 {
     mOwner          = theOwner;
     mOwnersProperty = mOwner.GetType().GetProperty(theOwnersPropertyName);
 }
예제 #3
0
        public void LoadObject(TestSequence testSequence)
        {
            reader = new StreamReader(mFile);

            Object theObject = null;

            Type[]   constructorParameterTypes = { typeof(TestSequence) };
            object[] constructorParameters     = { testSequence };
            do
            {
                // csline is case sensitive, while line is in lowercase
                string line = reader.ReadLine().Trim();

                try
                {
                    // Bypass comments and empty lines
                    if ((line.IndexOf("#") == 0) || (line.Length == 0))
                    {
                        continue;
                    }

                    if (line == "FavoriteSettings")
                    {
                        LoadFavoriteSettings(testSequence, reader);
                    }
                    else if (line == "FavoriteValues")
                    {
                        LoadFavoriteValues(testSequence, reader);
                    }
                    else if (line == "ImageViews")
                    {
                        LoadImageFormSettings(testSequence, reader);
                    }
                    else if (line.StartsWith("["))
                    {
                        theObject = null;
                        // http://www.eggheadcafe.com/articles/20050717.asp  (see comment about System.Activator.CreateInstance also)
                        if (!line.EndsWith("]"))
                        {
                            throw new Exception("invalid configuration line: '" + line + "'");
                        }
                        string typeName = line.Substring(1, line.Length - 2).Trim();

                        int dividerIndex = line.IndexOf("|");
                        if (dividerIndex >= 0)
                        {
                            string ownerLinkPortion = typeName.Substring(dividerIndex + 1).Trim();
                            typeName = typeName.Substring(0, dividerIndex - 1).Trim();
                            int               periodIndex       = ownerLinkPortion.IndexOf(".");
                            string            ownerName         = ownerLinkPortion.Substring(0, periodIndex).Trim();
                            string            ownerPropertyName = ownerLinkPortion.Substring(periodIndex + 1).Trim();
                            IObjectDefinition ownerObject       = testSequence.ObjectRegistry.GetObject(ownerName);
                            theObject = ownerObject.GetType().GetProperty(ownerPropertyName).GetValue(ownerObject, null); // zzz: for rectangle.bottom the type is "string" not "ImageObjectDef"...for previous owner relationships they were objects.  change one type?  different owner/parameter link style?
                        }
                        else
                        {
                            if (typeName == "RectangleROIDefinition")
                            {
                                typeName = typeName.Trim();
                                //typeName = "RectangleROIByEdgesDefinition"; // TODO: HACK: 20080618: so that old config files don't break
                            }
                            Type type = Type.GetType("NetCams." + typeName);
                            //                    theObject = Activator.CreateInstance(type);
                            if (type == null)
                            {
                                throw new ArgumentException("Type not found. type='" + typeName + "'");
                            }
                            ConstructorInfo ctorInfo = type.GetConstructor(constructorParameterTypes);
                            if (ctorInfo == null)
                            {
                                throw new ArgumentException("Not a valid type; type='" + typeName + "'; line = '" + line + "'");
                            }
                            theObject = ctorInfo.Invoke(constructorParameters);
                            if (theObject == null)
                            {
                                throw new ArgumentException("Can't instantiate type " + typeName);
                            }
                        }
                    }
                    else
                    {
                        if (theObject == null)
                        {
                            throw new ArgumentException("Config error: need to define object before properties; line = '" + line + "'");
                        }
                        int                    assignmentIndex       = line.IndexOf("=");
                        string                 propertyName          = line.Substring(0, assignmentIndex).Trim();
                        string                 propertyValueAsString = line.Substring(assignmentIndex + 1).Trim();
                        PropertyInfo           propInfo              = theObject.GetType().GetProperty(propertyName);
                        TypeConverter          converter             = TypeDescriptor.GetConverter(propInfo.PropertyType);
                        ITypeDescriptorContext context               = new ProjectContext(theObject);
                        object                 propertyValueAsObject = converter.ConvertFromString(context, propertyValueAsString);
                        propInfo.SetValue(theObject, propertyValueAsObject, null);
                    }
                }
                catch (ArgumentException e)
                {
                    testSequence.Window().logMessage("ERROR: problem loading test sequence configuration file.");
                    testSequence.Window().logMessage("ERROR: ...file='" + mFile + "'");
                    testSequence.Window().logMessage("ERROR: ...line='" + line + "'");
                    testSequence.Window().logMessage("ERROR: ...message='" + e.Message + "'");
                    Exception inner = e.InnerException;
                    while (inner != null)
                    {
                        testSequence.Window().logMessage("ERROR: ...inner message='" + inner.Message + "'");
                        inner = inner.InnerException;
                    }
                }
                catch (Exception e)
                {
                    testSequence.Window().logMessage("ERROR: exception loading test sequence configuration file.");
                    testSequence.Window().logMessage("ERROR: ...file='" + mFile + "'");
                    testSequence.Window().logMessage("ERROR: ...line='" + line + "'");
                    testSequence.Window().logMessage("ERROR: ...message='" + e.Message + "'");
                    Exception inner = e.InnerException;
                    while (inner != null)
                    {
                        testSequence.Window().logMessage("ERROR: ...inner message='" + inner.Message + "'");
                        inner = inner.InnerException;
                    }
                }
            } while (reader.Peek() != -1);

            reader.Close();
            reader.Dispose();
            reader = null;
        }