Skip to main content

Posts

Showing posts with the label reflection

Windows 8 Metro App - How to debug JS that was loaded at runtime

In the last period of time I had to work a lot with JavaScript and not for the UI, but for an entire Metro application on Windows 8. One of the nice features of JavaScript is the ability to access any kind of function and defining or overriding functions at runtime. This feature is very powerful but also can create a lot of mess. In this moment if we would try to write a C# Metro Application we would not have the ability to load at runtime any kind of assembly or code. The “reflection” part on the .NET framework for Metro Application is not the one that we know from the normal .NET. In JavaScript if we have a script like this: (function(){ “use strict” WinJS.Namespace.define(“MyClass”,{ doSomeAction: function(){ console.warn(“doSomeAction was called”); } }); })(); we could evaluate the script at runtime and call or instanced the object and functions that were defined. The first step is to evaluate the script. Let assume that the definition of this script can be foun...

How to get friendly format for GetType().Name

Pornim de la urmatorul exemplu: List<string> list = new List<string>(); Console.WriteLine(list.GetType().Name); Asa cum ne-am astepta ne este returnat "List`1". Dar uneori nu ne este de ajuns atata, vrem sa vedem si tipul generci. Ceva de genul acesta "List ". Pentru acest lucru avem nevoie sa apelam metoda GetGenericArguments pentru a obtine tipuriile generice. Mai jos gasiti un extension method la Type care returneaza numele la type asa cum l-am prezentat mai sus. public static class TypeExtensions { private const string TypeSeparator=","; private const char UnusedSymbol = '`'; private const string TypeFormat = "{0}<{1}>"; public static string ToGenericTypeString(this Type type) { if (!type.IsGenericType) { return type.Name; } return string.Format(TypeFormat, GetGenericTypeName(type), GetGenericArgumentsName(type)); } private static string GetGenericArgumentsName(...

Access a property using reflection

Cum putem face get/set la o proprietate prin intermediul la reflection. Odata ce avem tipul obiectului care contine proprietate, este de ajuns sa apelam InvokeMember si sa specificam numele proprietatii. private TReturn GetProperty<TReturn>(object obj, string propertyName) { return (TReturn) obj.GetType().InvokeMember( propertyName, BindingFlags.GetProperty, null, obj, null); } private void SetProperty(object obj, string propertyName, object value) { obj.GetType().InvokeMember( propertyName, BindingFlags.SetProperty, null, obj, new object[] { value }); } BindingsFlags se poate seta printr-un OR logic, in functie de proprietatea cu care lucram( publica, privata, statica, etc). De exemplu, pentru o proprietate publica si care nu este statica putem sa avem urmatorul apel: obj.GetType().InvokeMember( prope...

Adnotare enum cu mai multe valori

Cand dorim sa serializam un enum putem sa atasam la valoarea fiecarui item din enum atributul XmlEnumAttribute . Astfel la serializare/deserializare o sa putem lucra cu o valoarea string nu cu o valoare numerica. Pana aici nici o problema, dar cum facem sa obtinem doar aceasta valoarea si nu un xml care contine si aceasta valoarea . De exemplu pentru enum-ul: public enum Dimension { [XmlEnum(Name="BigT")] Big [XmlEnum(Name="SmallT")] Small } am obține folosindu-ne de XmlSerializer un output care contine si un nod care specifica ce tip de data ii. Pentru a putea sa obtinem doar valoare BigT sau SmallT putem sa apelam la reflection si sa iteram prin atributele item-ului respectiv pana ajungem la un atribut de tip XmlEnumAttribute . Acuma, daca ducem problema umpic mai departe, putem sa ajungem la cazuri in care elementele din enum au diferite valori. De exemplu daca lucram cu doua sau mai multe aplicații, putem sa avem doua sau mai multe reprezentări...