private static void Main(string[] args) { // verify that things work OK without reflection access var permission = new ReflectionPermission(ReflectionPermissionFlag.AllFlags); permission.Deny(); Console.WriteLine("Direct access"); TestDirect(1, false); // for JIT etc TestDirect(Cycles, true); Console.WriteLine(); string typeName = typeof(HyperTypeDescriptionProvider).Name; Console.WriteLine("Without " + typeName); RunTests(1, 1, false); // for JIT etc RunTests(MetaCycles, Cycles, true); HyperTypeDescriptionProvider.Add(typeof(MyEntity)); Console.WriteLine(); Console.WriteLine("With " + typeName); RunTests(1, 1, false); // for Emit, JIT etc RunTests(MetaCycles, Cycles, true); Console.ReadLine(); }
public static object Bind(Type obj, NameValueCollection values, CultureInfo culture) { object component = obj.GetConstructor(new Type[0]).Invoke(new object[0]); PropertyDescriptorCollection properties = new HyperTypeDescriptionProvider(obj).GetTypeDescriptor(obj).GetProperties(); foreach (string allKey in values.AllKeys) { PropertyDescriptor propertyDescriptor = properties.Find(allKey, true); if (propertyDescriptor != null && !propertyDescriptor.IsReadOnly) { if (propertyDescriptor.Converter.CanConvertFrom(typeof(string))) { try { int result; if (allKey != "id" || allKey == "id" && int.TryParse(values[allKey], out result)) { propertyDescriptor.SetValue(component, propertyDescriptor.Converter.ConvertFrom((ITypeDescriptorContext)null, culture, (object)values[allKey])); } else { propertyDescriptor.SetValue(component, propertyDescriptor.ComponentType.IsValueType ? Activator.CreateInstance(propertyDescriptor.ComponentType) : (object)(obj = (Type)null)); } } catch { } } } } return(component); }
static void Main() { HyperTypeDescriptionProvider.Add(typeof(Person)); var properties = new Dictionary<string, object> { { "Id", 10 }, { "Name", "Fred Flintstone" } }; Person person = new Person(); DynamicUpdate(person, properties); Console.WriteLine("Id: {0}; Name: {1}", person.Id, person.Name); Console.ReadKey(); }
public void GlobalSetupAttribute() { genarator = new FakeGenarator(); sampleRecords = Enumerable.Range(0, Program.iterations).Select(genarator.Generate).ToArray(); if (useHyperTypeDescriptionProvider) { HyperTypeDescriptionProvider.Add(typeof(FixedSampleRecord)); } }
public List <object> GetOccurrences(IEnumerable items, DateTime from, DateTime to) { List <object> objectList = new List <object>(); bool flag = true; List <DHXEventsHelper._ParsedEventData> source = new List <DHXEventsHelper._ParsedEventData>(); PropertyDescriptorCollection info = (PropertyDescriptorCollection)null; foreach (object ev in items) { if (flag) { info = new HyperTypeDescriptionProvider(ev.GetType()).GetTypeDescriptor(ev.GetType()).GetProperties(); flag = false; } DHXEventsHelper._ParsedEventData occurrences = this._GetOccurrences(ev, from, to, 300, info); if (occurrences != null) { source.Add(occurrences); } } TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now); using (List <DHXEventsHelper._ParsedEventData> .Enumerator enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { DHXEventsHelper._ParsedEventData eventSet = enumerator.Current; if (eventSet.EventInfo.parsed_type == null || eventSet.EventInfo.parsed_type.Type == RecTypes.None || string.IsNullOrEmpty(eventSet.EventInfo.rec_type)) { DHXEventsHelper._ParsedEventData parsedEventData = source.SingleOrDefault <DHXEventsHelper._ParsedEventData>((Func <DHXEventsHelper._ParsedEventData, bool>)(set => { int id = set.EventInfo.id; int?eventPid = eventSet.EventInfo.event_pid; if (id == eventPid.GetValueOrDefault()) { return(eventPid.HasValue); } return(false); })); if (parsedEventData != null && eventSet.EventInfo.event_length.HasValue) { DateTime dateTime = new DateTime(1970, 1, 1).AddSeconds((double)eventSet.EventInfo.event_length.Value) + utcOffset; parsedEventData.Occurrences.Remove(dateTime); } } } } foreach (DHXEventsHelper._ParsedEventData data in source) { if (data.EventInfo.parsed_type == null && string.IsNullOrEmpty(data.EventInfo.rec_type) || data.EventInfo.parsed_type != null && (data.EventInfo.parsed_type.Type == RecTypes.Invalid || data.EventInfo.parsed_type.Type != RecTypes.None)) { objectList.AddRange((IEnumerable <object>) this._ProcessEvData(data, info)); } } return(objectList); }
private List <string> DumpToLabels(object inst, Type type) { List <StringBuilder> builders = new List <StringBuilder>(); builders.Add(new StringBuilder()); int row = 0; int column = 0; int rows = this.Height / 12 - 6; // Make it go FAST HyperTypeDescriptionProvider.Add(type); PropertyDescriptorCollection PropertyDescriptors = TypeDescriptor.GetProperties(type); PropertyInfo[] pic = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in pic) { PropertyDescriptor fi = PropertyDescriptors[pi.Name]; object value = fi.GetValue(inst); string txt = ""; if (fi.PropertyType == typeof(double)) { txt = string.Format("{0} = {1} ", pi.Name, Math.Round((double)value, 3)); } else if (fi.PropertyType == typeof(float)) { txt = string.Format("{0} = {1} ", pi.Name, Math.Round((float)value, 3)); } else if (value != null) { txt = string.Format("{0} = {1} ", pi.Name, value.ToString()); } builders[column].AppendLine(txt); if (txt.Length > 30) { row++; // double rows } row++; // Go to next side. if (row == rows) { builders.Add(new StringBuilder()); column++; row = 0; } } return(builders.Select(x => x.ToString()).ToList()); }
public void BigDataWriteOperationShouldBeQuickWithReflectionMagic() { HyperTypeDescriptionProvider.Add(typeof(FixedSampleRecord)); BigDataWriteOperationShouldBeQuick(); }
public TelemetryLoggerSubscribedInstance(Type type, object instance) { this.type = type; this.instance = instance; RunFrequency = 100.0; HyperTypeDescriptionProvider.Add(type); Events = new Dictionary <string, List <List <string> > >(); LogOnChange = new Dictionary <string, bool>(); LogOnChangePreviousValue = new Dictionary <string, object>(); Mapping = new Dictionary <string, int>(); Frequencies = new Dictionary <string, double>(); FrequencyCounter = new Dictionary <string, int>(); // Fill things. int indexer = 0; double Frequency; PropertyDescriptors = TypeDescriptor.GetProperties(type); PropertyInfo[] pic = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in pic) { PropertyDescriptor fi = PropertyDescriptors[pi.Name]; if (fi == null || fi.Attributes.Contains(new Unloggable())) { continue; } // ---------- Unloggable ---------- object[] attrs = pi.GetCustomAttributes(typeof(Unloggable), false); if (attrs.Length == 1)// is unloggable? { continue; } // ---------- Frequency ---------- attrs = pi.GetCustomAttributes(typeof(Loggable), false); if (attrs.Length == 1) { Frequency = ((Loggable)attrs[0]).Freqency; } else { Frequency = 100.0; // 100Hz normal } Frequencies.Add(fi.Name, Frequency); FrequencyCounter.Add(fi.Name, 0); // ---------- Log on change ---------- attrs = pi.GetCustomAttributes(typeof(LogOnChange), false); LogOnChange.Add(fi.Name, ((attrs.Length == 1) ? true : false)); LogOnChangePreviousValue.Add(fi.Name, null); // ---------- Events ---------- attrs = pi.GetCustomAttributes(typeof(LogOnEvent), false); Events.Add(fi.Name, new List <List <string> >()); if (attrs.Length > 0) { foreach (LogOnEvent logEvent in attrs) { Events[fi.Name].Add(logEvent.Events); } } // ---------- Mapping ---------- indexer++; Mapping.Add(fi.Name, indexer); } }