public void Bind(string path, object target) { PropertyInfo[] propertiesInfo = target.GetType().GetProperties(); // loop through all properties and look for ones marked with attributes foreach (PropertyInfo propInfo in propertiesInfo) { // get as many script attributes as there are on this type /* TODO I dont understand why this code doesnt work....??? * ScriptAttribute[] scriptAtts = * (ScriptAttribute[])propInfo.GetCustomAttributes(typeof(ScriptAttribute), true); */ ScriptAttribute[] scriptAtts = (ScriptAttribute[])Attribute.GetCustomAttributes(propInfo, typeof(ScriptAttribute), true); // loop through each one we found and register its command for (int j = 0; j < scriptAtts.Length; j++) { ScriptAttribute scriptAtt = scriptAtts[j]; if (path != null && path.Length != 0) { Tie(path + "/" + scriptAtt.Name, target, propInfo, false); } else { Tie(scriptAtt.Name, target, propInfo, false); } } // for } // for }
/** * Returns: Collection of Test names * Params: * attribute: the Script attribute that will determine the column of values to be returned * default: TestNames * */ public static ObservableCollection <string> GetTestCollections(ScriptAttribute attribute, SqlConnection cnn) { ObservableCollection <string> list = new ObservableCollection <string>(); using (var cmd = new SqlCommand("", cnn)) { switch (attribute) { case ScriptAttribute.TestNames: cmd.CommandText = "USE WLScriptsDB SELECT DISTINCT test_name FROM Tests";; break; case ScriptAttribute.BuildNames: cmd.CommandText = "USE WLScriptsDB SELECT DISTINCT build_version FROM Tests";; break; default: cmd.CommandText = "USE WLScriptsDB SELECT DISTINCT test_name FROM TESTS";; break; } using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { list.Add(reader.GetString(0)); } } } return(list); }
/// <summary> /// Constructor /// </summary> public PropertyManager(bool buildStaticMethods) { if (buildStaticMethods) { Type[] types = Assembly.GetCallingAssembly().GetTypes(); // loop through all types and look for properties foreach (Type type in types) { PropertyInfo[] propertiesInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Static); // loop through all properties and look for ones marked with attributes foreach (PropertyInfo propInfo in propertiesInfo) { // get as many script attributes as there are on this type ScriptAttribute[] scriptAtts = (ScriptAttribute[])propInfo.GetCustomAttributes(typeof(ScriptAttribute), true); // loop through each one we found and register its command for (int j = 0; j < scriptAtts.Length; j++) { ScriptAttribute scriptAtt = scriptAtts[j]; TieStatic(scriptAtt.Name, type, propInfo, false); } // for } // for } } }
/// <summary> /// Yrittää kääntää assemblyn, jos virheitä ilmenee, logger /// näyttää errorit userille. /// </summary> /// <param name="fullname">Scriptin koko nimi (path + filename + extension)</param> /// <returns>Käännetty assembly tai null jos kääntäminen ei onnistu.</returns> public ScriptAssembly CompileScript(string fullname, string scriptName) { CompilerResults compilerResults = null; using (CSharpCodeProvider csharpCompiler = new CSharpCodeProvider()) { compilerResults = csharpCompiler.CompileAssemblyFromFile(GenerateCompilerOptions(), fullname); // Jos kääntämisen yhteydessä ilmenee virheitä, annetaan loggerin handlata errorit // ja asetetaan resultit nulliksi. if (compilerResults.Errors.HasErrors) { compilerErrorLogger.ShowErrors(compilerResults.Errors, fullname); compilerResults = null; } else { List <Type> scripts = compilerResults.CompiledAssembly .GetTypes() .Where(c => c.GetInterface("IScript", true) != null) .ToList(); for (int i = 0; i < scripts.Count; i++) { ScriptAttribute attribute = scripts[i].GetCustomAttributes(false) .FirstOrDefault(a => a.GetType() == typeof(ScriptAttribute)) as ScriptAttribute; if (attribute != null && !attribute.IsHidden) { string[] methods = scripts[i].GetMethods().Select <MethodInfo, string>(m => m.Name).ToArray(); string[] members = scripts[i].GetMembers().Select <MemberInfo, string>(m => m.Name).ToArray(); for (int j = 0; j < ScriptTypes.Count; j++) { string[] myMethods = ScriptTypes[j].GetMethods().Select <MethodInfo, string>(m => m.Name).ToArray(); string[] myMembers = ScriptTypes[j].GetMembers().Select <MemberInfo, string>(m => m.Name).ToArray(); if (Array.TrueForAll(methods, m => myMethods.Contains(m)) && Array.TrueForAll(members, m => myMembers.Contains(m))) { return(new ScriptAssembly(ScriptTypes.Find(t => t.Name == scriptName), scriptName, fullname)); } } } } } } return(compilerResults == null ? null : new ScriptAssembly(compilerResults.CompiledAssembly, scriptName, fullname)); }
public ScriptItemListWindow(ScriptAttribute attribute) { InitializeComponent(); DataContext = this; switch (attribute) { case ScriptAttribute.TestNames: SelectableList = AttributesRepository.Repository.TestNames; break; case ScriptAttribute.BuildNames: SelectableList = AttributesRepository.Repository.TestBuilds; break; case ScriptAttribute.ScenarioNames: SelectableList = AttributesRepository.Repository.ScriptNames; break; } }
public static void Serialize <T>(T obj, Stream s, char QouteChar) { Action <char> WriteChar = c => s.WriteByte((byte)c); Action <string> WriteChars = delegate(string x) { foreach (char v in x) { WriteChar(v); } }; var h = new Hashtable(); Action <object> WriteObject = null; Action <Array> WriteArray = null; Action <string> WriteQuotedString = null; Action <int> WriteInt32 = null; Action <Type, Func <object> > WriteElement = null; #region WriteQuotedString WriteQuotedString = delegate(string x) { if (x == null) { WriteChars("null"); return; } WriteChar(QouteChar); foreach (char v in x) { switch (v) { case '\n': WriteChars(@"\n"); continue; case '\t': WriteChars(@"\t"); continue; case '\r': WriteChars(@"\r"); continue; case '\b': WriteChars(@"\b"); continue; case '"': WriteChars("\\\""); continue; case '\'': WriteChars("\\\'"); continue; default: if (char.IsLetterOrDigit(v) || char.IsPunctuation(v) || v == ' ') { WriteChar(v); } else if (v > 0xFF) { WriteChars(string.Format(@"\u{0:x4}", (int)v)); } else { WriteChars(string.Format(@"\x{0:x2}", (int)v)); } continue; } } WriteChar(QouteChar); }; #endregion WriteInt32 = delegate(int x) { foreach (char v in x.ToString()) { WriteChar(v); } }; #region WriteArray WriteArray = delegate(Array x) { if (x == null) { WriteChars("null"); return; } WriteChar('['); for (int i = 0; i < x.Length; i++) { object v = x.GetValue(i); if (i > 0) { WriteChar(','); } if (v == null) { WriteChars("null"); continue; } WriteElement(v.GetType(), () => v); } WriteChar(']'); }; #endregion Action <object> WriteAnyElement = v => WriteElement(v.GetType(), () => v); WriteElement = delegate(Type ft, Func <object> fv) { if (ft == typeof(FunctionReturnValue)) { WriteChars("function (){return "); WriteAnyElement(((FunctionReturnValue)fv()).GetValue()); WriteChars(";}"); } else if (ft == typeof(LiteralString)) { WriteChars((LiteralString)fv()); } else if (ft == typeof(string)) { WriteQuotedString((string)fv()); } else if (ft == typeof(int)) { WriteInt32((int)fv()); } else if (ft.IsArray) { WriteArray((Array)fv()); } else if (ft.IsClass) { WriteObject(fv()); } else { throw new NotImplementedException(); } }; #region WriteObject WriteObject = delegate(object x) { if (x == null) { WriteChars("null"); return; } WriteChar('{'); var f = x.GetType().GetFields().ToArray(); var fieldindex = -1; for (int i = 0; i < f.Length; i++) { var v = f[i]; if (fieldindex++ > -1) { WriteChar(','); } //WriteQuotedString(v.Name); WriteChars(v.Name); WriteChar(':'); WriteElement(v.FieldType, () => v.GetValue(x)); } if (ScriptAttribute.IsAnonymousType(x.GetType())) { var p = x.GetType().GetProperties().ToArray(); foreach (var v in p) { if (fieldindex++ > -1) { WriteChar(','); } WriteChars(v.Name); WriteChar(':'); WriteElement(v.PropertyType, () => v.GetValue(x, null)); } } WriteChar('}'); }; #endregion WriteAnyElement(obj); }
public static ScriptAttribute ToScriptAttributeOrDefault(this ICustomAttributeProvider p) { return(ScriptAttribute.OfProvider(p) ?? new ScriptAttribute()); }
public static ScriptAttribute ToScriptAttribute(this ICustomAttributeProvider p) { return(ScriptAttribute.OfProvider(p)); }