public static void Main(string[] args) { object testFinance = CreateInstance("Finance"); Console.WriteLine(testFinance.GetType().GetProperty("taxRate")); Finance testFinance2 = new Finance(); foreach (FieldInfo FI in testFinance.GetType().GetFields()) { if (FI.Name == "bankSide") { dynamic bankSide = FI.GetValue(testFinance); List <Bank> bankSide1 = bankSide; foreach (FieldInfo BI in bankSide[0].GetType().GetFields()) { if (BI.Name == "branchID") { dynamic branchID = BI.GetValue(bankSide1[0]); List <int> branchID1 = branchID; foreach (int i in branchID1) { Console.WriteLine(i); } } } // Console.WriteLine(test); } } //Console.WriteLine(myPropInfo.GetValue(testFinance2, null)); Console.ReadKey(); }
/// <summary> /// Evaluates the node, using the variables provided in the <paramref name="Variables"/> collection. /// </summary> /// <param name="Variables">Variables collection.</param> /// <returns>Result.</returns> public override IElement Evaluate(Variables Variables) { IElement E = this.Argument.Evaluate(Variables); object Obj = E.AssociatedObjectValue; List <IElement> Elements = new List <IElement>(); if (Obj is Type T) { foreach (FieldInfo FI in T.GetTypeInfo().DeclaredFields) { Elements.Add(new StringValue(FI.Name)); } return(new ObjectVector(Elements)); } else { T = Obj.GetType(); foreach (FieldInfo FI in T.GetTypeInfo().DeclaredFields) { Elements.Add(new StringValue(FI.Name)); Elements.Add(Expression.Encapsulate(FI.GetValue(Obj))); } ObjectMatrix M = new ObjectMatrix(Elements.Count / 2, 2, Elements) { ColumnNames = new string[] { "Name", "Value" } }; return(M); } }
/// <summary> /// This method is used to set the DirtyStatus to NoChange to item and it's child items /// </summary> public void SetDirtyStatusToNoChange() { DirtyStatus = eDirtyStatus.NoChange; // Properties foreach (PropertyInfo PI in this.GetType().GetProperties()) { var token = PI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { continue; } if (typeof(IObservableList).IsAssignableFrom(PI.PropertyType)) { IObservableList obj = (IObservableList)PI.GetValue(this); if (obj == null) { continue; } foreach (object o in obj) { if (o is RepositoryItemBase) { ((RepositoryItemBase)o).SetDirtyStatusToNoChange(); } } } } // Fields foreach (FieldInfo FI in this.GetType().GetFields()) { var token = FI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { continue; } if (typeof(IObservableList).IsAssignableFrom(FI.FieldType)) { IObservableList obj = (IObservableList)FI.GetValue(this); if (obj == null) { return; } foreach (object o in obj) { if (o is RepositoryItemBase) { ((RepositoryItemBase)o).SetDirtyStatusToNoChange(); } } } } }
public override string ToString() { string FieldNames = string.Empty; // = "Name=" + Name +"\n"; FieldInfo[] ProtocolFields = ProtocolProvider.GetProtocolParametersFields(this); foreach (FieldInfo FI in ProtocolFields) { FieldNames += string.Format("{0}={1}\n", FI.Name, FI.GetValue(this).ToString()); } return(FieldNames); }
public static string GetListDataFromField <From>(From obj, string Delim = "_") { FieldInfo[] FS = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); StringBuilder s = new StringBuilder(100); foreach (FieldInfo FI in FS) { s.Append(FI.Name); s.Append(":"); s.Append(FI.GetValue(obj).ToString()); s.Append(Delim); } string S = s.ToString(); return(S.Substring(0, S.Length - Delim.Length)); }
static IEnumerable <Sprite> getSprites(Holdable e) { Type type = e.GetType(); List <FieldInfo> fields; if (!SpriteFields.TryGetValue(type, out fields)) { SpriteFields.Add(type, fields = type.GetFields(flags).Where(field => typeof(Sprite).IsAssignableFrom(field.FieldType)).ToList()); } return(fields.Select(FI => (Sprite)FI.GetValue(e))); }
public static NamedEnum[] GetAll(Type T) { if (!T.IsEnum) { return(null); } ArrayList Values = new ArrayList(); foreach (FieldInfo FI in T.GetFields(BindingFlags.Public | BindingFlags.Static)) { Values.Add(new NamedEnum(FI.GetValue(null))); } return((NamedEnum[])Values.ToArray(typeof(NamedEnum))); }
public string ToJson <T>(Type Instance) { int i = 0; string[] paramNames = new string[typeof(T).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Length]; string[] paramValues = new string[typeof(T).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Length]; // {"type":"analog", "table":"norm", "column":"diFlourHopper_Mass", "decimal":3, "color":#88FF00, "coef":0.001 } string json = "{"; switch (Instance.GetType().FullName) #pragma warning disable CS1522 // Empty switch block { #pragma warning restore CS1522 // Empty switch block } foreach (FieldInfo FI in typeof(T).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { string param = FI.Name.ToLower(); object obj = FI.GetValue(this); string paramValue = obj.ToString(); paramNames[i] = param; paramValues[i] = paramValue; json += "\"" + paramNames[i] + "\":"; switch (FI.FieldType.Name) { case "String": json += "\"" + paramValues[i] + "\", "; break; case "Color": Color color = (Color)FI.GetValue(this); if (color != null) { json += ColorTranslator.ToHtml(color).ToString() + ", "; } break; default: json += paramValues[i] + ", "; break; } i++; } json = json.Remove(json.LastIndexOf(", "), 2); json += "}"; return(json); }
private static List <string[]> ObserveVariables() { // Get a list of all the variables that are being observed and their contents var ReflectionTest = typeof(User_Program).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); var Stringses = new List <string[]>(); foreach (var FI in ReflectionTest) { Stringses.Add(new[] { FI.Name, (FI.GetValue(null) ?? "null").ToString(), FI.FieldType.Name }); } return(Stringses); }
/// <summary> /// <see cref="IDisposable.Dispose"/> /// </summary> public override void Dispose() { if (!(this.w is null)) { this.w.Flush(); this.output.Flush(); if (this.cs != null && this.blockSize > 1) { Type T = typeof(CryptoStream); int i; FieldInfo FI = null; PropertyInfo PI = T.GetProperty("_inputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic); if (PI is null) { FI = T.GetField("_inputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic); if (FI is null) { PI = T.GetProperty("_InputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic); if (PI is null) { FI = T.GetField("_InputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic); } } } do { if (!(PI is null)) { i = (int)PI.GetValue(this.cs); } else if (!(FI is null)) { i = (int)FI.GetValue(this.cs); }
public void CheckPropertyChangedTriggered() { // Scan all RIs for each prop marked with [IsSerializedForLocalRepositoryAttribute] try to change and verify prop changed triggered //Arrange // Get all Repository items IEnumerable <Type> list = GetRepoItems(); ErrCounter = 0; //Act foreach (Type type in list) { Console.WriteLine("CheckPropertyChangedTriggered for type: " + type.FullName); if (type.IsAbstract) { continue; } RepositoryItemBase RI = (RepositoryItemBase)Activator.CreateInstance(type); RI.PropertyChanged += RIPropertyChanged; RI.StartDirtyTracking(); // Properties foreach (PropertyInfo PI in RI.GetType().GetProperties()) { var token = PI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { continue; } Console.WriteLine("CheckPropertyChangedTriggered for property: " + PI.Name); object newValue = GetNewValue(PI.PropertyType, PI.GetValue(RI)); if (newValue != null) { RI.DirtyStatus = eDirtyStatus.NoChange; prop = null; PI.SetValue(RI, newValue); CheckChanges(RI, PI.Name, newValue); } } // Fields foreach (FieldInfo FI in RI.GetType().GetFields()) { var token = FI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { continue; } Console.WriteLine("CheckPropertyChangedTriggered for property: " + FI.Name); object newValue = GetNewValue(FI.FieldType, FI.GetValue(RI)); if (newValue != null) { RI.DirtyStatus = eDirtyStatus.NoChange; prop = null; FI.SetValue(RI, newValue); CheckChanges(RI, FI.Name, newValue); } } } //Assert Assert.AreEqual(0, ErrCounter); }
//[Ignore] //[TestMethod] [Timeout(60000)] //public void NewRepositorySerializer_ReadOldXML() //{ // // Using new SR2 to load and write old XML but load old object, save with the style // //Arrange // //GingerCore.Repository.RepositorySerializerInitilizer OldSR = new GingerCore.Repository.RepositorySerializerInitilizer(); // //GingerCore.Repository.RepositorySerializerInitilizer.InitClassTypesDictionary(); // NewRepositorySerializer RS2 = new NewRepositorySerializer(); // string fileName = Common.getGingerUnitTesterDocumentsFolder() + @"Repository\BigFlow1.Ginger.BusinessFlow.xml"; // //Act // string txt = System.IO.File.ReadAllText(fileName); // BusinessFlow BF = (BusinessFlow)NewRepositorySerializer.DeserializeFromText(txt); // //load with new // // BusinessFlow BF = (BusinessFlow)RS2.DeserializeFromFile(fileName); // //Serialize to new style // //string s = RS2.SerializeToString(BF); // // cretae from new style SR2 // BusinessFlow BF2 = (BusinessFlow)RS2.DeserializeFromText(typeof(BusinessFlow), txt, filePath: fileName); // //to test the compare change something in b like below // // BF2.Activities[5].Description = "aaa"; // // BF2.Activities.Remove(BF2.Activities[10]); // //Assert // // System.IO.File.WriteAllText(@"c:\temp\BF1.xml", s); // // Assert.AreEqual(78, BF.Activities.Count); // //Assert.AreEqual(78, BF2.Activities.Count); // CompareRepoItem(BF, BF2); //} private void CompareRepoItem(RepositoryItemBase a, RepositoryItemBase b) { var props = a.GetType().GetProperties(); foreach (PropertyInfo PI in props) { var token = PI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token != null) { Console.WriteLine("compare: " + a.ToString() + " " + PI.Name); object aProp = PI.GetValue(a); object bProp = b.GetType().GetProperty(PI.Name).GetValue(b); if (aProp == null && bProp == null) { continue; } if (aProp.ToString() != bProp.ToString()) { throw new Exception("Items no match tostring: " + a.ItemName + " attr: " + PI.Name + " a=" + aProp.ToString() + " b=" + bProp.ToString()); } //if (aProp != bProp) //{ // throw new Exception("Items no match: " + a.ItemName + " attr: " + PI.Name + " a=" + aProp.ToString() + " b=" + bProp.ToString()); //} } } var fields = a.GetType().GetFields(); foreach (FieldInfo FI in fields) { var token = FI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token != null) { Console.WriteLine("compare: " + a.ToString() + " " + FI.Name); object aFiled = FI.GetValue(a); object bField = b.GetType().GetField(FI.Name).GetValue(b); if (aFiled == null && bField == null) { continue; } if (aFiled.ToString() != bField.ToString()) { throw new Exception("Items no match tostring: " + a.ItemName + " attr: " + FI.Name + " a=" + aFiled.ToString() + " b=" + bField.ToString()); } //if (aFiled != bField) //{ // throw new Exception("Items no match: " + a.ItemName + " attr: " + FI.Name + " a=" + aFiled.ToString() + " b=" + bField.ToString()); //} if (aFiled is IObservableList) { if (((IObservableList)aFiled).Count != ((IObservableList)bField).Count) { throw new Exception("Items in list count do not match: " + a.ItemName + " attr: " + FI.Name + " a=" + aFiled.ToString() + " b=" + bField.ToString()); } var aList = ((IObservableList)aFiled).GetEnumerator(); var bList = ((IObservableList)bField).GetEnumerator(); while (aList.MoveNext()) { bList.MoveNext(); RepositoryItemBase o1 = (RepositoryItemBase)aList.Current; RepositoryItemBase o2 = (RepositoryItemBase)bList.Current; CompareRepoItem(o1, o2); } } } } }
/// <summary> /// Advances the enumerator to the next element of the collection. /// </summary> /// <returns>true if the enumerator was successfully advanced to the next element; false if /// the enumerator has passed the end of the collection.</returns> /// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created.</exception> public async Task <bool> MoveNextAsync() { Dictionary <string, List <object> > Aggregated = null; ObjectProperties Variables = null; IElement E; object[] Last = null; int i, c = this.groupBy.Length; object o1, o2; while (this.processLast || await e.MoveNextAsync()) { this.processLast = false; Variables = new ObjectProperties(e.Current, this.variables); if (Last is null) { Last = new object[c]; for (i = 0; i < c; i++) { E = this.groupBy[i].Evaluate(Variables); Last[i] = E.AssociatedObjectValue; } } else { for (i = 0; i < c; i++) { E = this.groupBy[i].Evaluate(Variables); o1 = Last[i]; o2 = E.AssociatedObjectValue; if (o1 is null ^ o2 is null) { break; } if (o1 != null && !o1.Equals(o2)) { break; } } if (i < c) { this.processLast = true; break; } } o1 = e.Current; Type T = o1.GetType(); if (T != this.lastType) { this.lastType = T; this.properties = T.GetRuntimeProperties(); this.fields = T.GetRuntimeFields(); } if (Aggregated is null) { Aggregated = new Dictionary <string, List <object> >(); } foreach (PropertyInfo PI in this.properties) { if (!PI.CanRead || !PI.CanWrite) { continue; } if (!Aggregated.TryGetValue(PI.Name, out List <object> List)) { List = new List <object>(); Aggregated[PI.Name] = List; } List.Add(PI.GetValue(o1)); } foreach (FieldInfo FI in this.fields) { if (!Aggregated.TryGetValue(FI.Name, out List <object> List)) { List = new List <object>(); Aggregated[FI.Name] = List; } List.Add(FI.GetValue(o1)); } } if (Aggregated is null) { return(false); } Dictionary <string, object> Result = new Dictionary <string, object>(); bool First = true; foreach (KeyValuePair <string, List <object> > Rec in Aggregated) { object[] A = Rec.Value.ToArray(); Result[Rec.Key] = A; if (First) { First = false; Result[" First "] = A; } } if (this.groupNames != null) { for (i = 0; i < c; i++) { ScriptNode Node = this.groupNames[i]; if (Node is null) { continue; } if (Node is VariableReference Ref) { Result[Ref.VariableName] = Last[i]; } else { E = this.groupNames[i]?.Evaluate(Variables); if (E != null && E is StringValue S) { Result[S.Value] = Last[i]; } } } } this.current = Result; return(true); }
public void StartDirtyTracking() { if (DirtyStatus != eDirtyStatus.NoTracked) { // Nothing to do return; } DirtyTrackingFields = new List <string>(); DirtyStatus = eDirtyStatus.NoChange; //first track self item changes PropertyChanged += ItmePropertyChanged; // now track all children which are marked with isSerizalized... // throw err if item is serialized but dindn't impl IsDirty // Properties foreach (PropertyInfo PI in this.GetType().GetProperties()) { var token = PI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { continue; } DirtyTrackingFields.Add(PI.Name); // We track observable list which are seriazlized - drill down recursivley in obj tree if (typeof(IObservableList).IsAssignableFrom(PI.PropertyType)) { IObservableList obj = (IObservableList)PI.GetValue(this); if (obj == null) { return; } TrackObservableList((IObservableList)obj); } } // Fields foreach (FieldInfo FI in this.GetType().GetFields()) { var token = FI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { continue; } DirtyTrackingFields.Add(FI.Name); // We track observable list which are seriazlized - drill down recursivley in obj tree if (typeof(IObservableList).IsAssignableFrom(FI.FieldType)) { IObservableList obj = (IObservableList)FI.GetValue(this); if (obj == null) { return; } TrackObservableList((IObservableList)obj); } } }
public void StartDirtyTracking() { if (DirtyStatus != eDirtyStatus.NoTracked) { // Nothing to do return; } DirtyTrackingFields = new ConcurrentBag <string>(); DirtyStatus = eDirtyStatus.NoChange; //first track self item changes PropertyChanged += ItmePropertyChanged; // now track all children which are marked with isSerizalized... // throw err if item is serialized but dindn't impl IsDirty // Properties Parallel.ForEach(this.GetType().GetProperties(), PI => { var token = PI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { return; } DirtyTrackingFields.Add(PI.Name); // We track observable list which are seriazlized - drill down recursivley in obj tree if (typeof(IObservableList).IsAssignableFrom(PI.PropertyType)) { //skip list if it is LazyLoad and was not loaded yet var lazyLoadtoken = PI.GetCustomAttribute(typeof(IsLazyLoadAttribute)); if (lazyLoadtoken != null) { string lazyStatusProp = PI.Name + nameof(IObservableList.LazyLoad); if (this.GetType().GetProperty(lazyStatusProp) != null) { if (bool.Parse(this.GetType().GetProperty(PI.Name + nameof(IObservableList.LazyLoad)).GetValue(this).ToString()) == true) { return;//skip doing dirty tracking for observableList which is LazyLoad and not loaded yet } } else { Reporter.ToLog(eLogLevel.ERROR, string.Format("Failed to check if to start DirtyTracking for Lazy Load ObservabelList called '{0}' because the property '{1}' is missing", PI.Name, lazyStatusProp)); } } IObservableList obj = (IObservableList)PI.GetValue(this); if (obj == null) { return; } TrackObservableList((IObservableList)obj); } }); // Fields Parallel.ForEach(this.GetType().GetFields(), FI => { var token = FI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { return; } DirtyTrackingFields.Add(FI.Name); // We track observable list which are seriazlized - drill down recursivley in obj tree if (typeof(IObservableList).IsAssignableFrom(FI.FieldType)) { IObservableList obj = (IObservableList)FI.GetValue(this); if (obj == null) { return; } TrackObservableList((IObservableList)obj); } }); }
private static string objectToString(object Input, string partial, int lvl) { if (Input == null) { return("NULL"); } //if it's just a regular thing then is easy if (Input.GetType().IsPrimitive || Input is string) { return(Input.ToString()); } //brings in whatever we have so far, we will add to this. string output = partial; //check all the fields foreach (FieldInfo FI in Input.GetType().GetFields()) { //if it has useful children, record this name and get the children information FieldInfo[] f = FI.FieldType.GetFields(); if (FI.FieldType.IsPrimitive || FI.FieldType == typeof(string) || FI.FieldType == typeof(decimal) || FI.FieldType == typeof(DateTime)) { output += new string('>', lvl) + FI.Name + " : " + FI.GetValue(Input) + "\r\n"; } //If it is a collection poop out all the items else if (FI.GetValue(Input) is System.Collections.ICollection) { string part = new string('>', lvl) + FI.Name + " : \r\n"; output += part; int Count = 0; foreach (object o in (FI.GetValue(Input) as System.Collections.ICollection)) { part = string.Format("{0} {1}[{2}]: \r\n", new string('>', lvl + 1), o.GetType(), Count++); output += objectToString(o, part, lvl + 2); } } else { string part = new string('>', lvl) + FI.Name + " : \r\n"; output += objectToString(FI.GetValue(Input), part, lvl + 1); } } foreach (PropertyInfo PI in Input.GetType().GetProperties()) { if (PI.PropertyType.IsPrimitive || PI.PropertyType == typeof(string) || PI.PropertyType == typeof(decimal)) { output += new string('>', lvl) + PI.Name + " : " + PI.GetValue(Input) + "\r\n"; } //If it is a collection poop out all the items else if (PI.GetValue(Input) is System.Collections.ICollection) { string part = new string('>', lvl) + PI.Name + " : \r\n"; output += part; int Count = 0; foreach (object o in (PI.GetValue(Input) as System.Collections.ICollection)) { part = string.Format("{0} {1}[{2}]: \r\n", new string('>', lvl + 1), o.GetType(), Count++); output += objectToString(o, part, lvl + 2); } } else { string part = new string('>', lvl) + PI.Name + " : \r\n"; output += objectToString(PI.GetValue(Input), part, lvl + 1); } //System.Windows.Forms.MessageBox.Show(temp); } //all done return(output); }