vendredi 19 août 2016

How can I get the value of properties in nested classes using Reflection?

I realise the question sounds similar to some others, but they don't really answer what I need. I'm trying to use Reflection to put a big "data" class which has other data classes within it and get all these values to quickly create a JSON so that every time a new piece of data is added there is no need to re-code anything.

So, basically it would look something like this:

public class A
{
    public int data1 { set; get; }
    public B b { set; get; }
}
public class B
{
    public int data2 { set; get; }
}

//main method
{
    A a = new A();
    a.data1 = 1;
    a.b = new B();
    a.b.data2 = 2;

    PropertyInfo[] propInfos = typeof(A).GetProperties();
    foreach (PropertyInfo propInfo in propInfos)
    {
        // ??
        //if (!propInfo.GetType().IsPrimitive)
        //{
        //    PropertyInfo[] propInfosForB = propInfo.GetType().GetProperties();
        //    foreach (PropertyInfo propInfoForB in propInfos)
        //    {
        //        print(propInfoForB.GetValue(propInfo, null));
        //    }
        //}
        //else
        {               
            print(propInfo.GetValue(a, null));
        }
    }
}

So basically "1" prints fine, but I can't figure out how to get "2" from B (as part of A). All I get is loads of "1" and "B" printed or errors. Hope that makes sense.

P.S: I know serialization would be a simpler solution to put all this in a JSON, but I can't use that for different reasons.





Instantiate dynamic object in java [duplicate]

This question already has an answer here:

I would like to ask you guys if how can I instantiate an object from a string?

Ex:

Methods meth = new MethodsImpl();

Note: Methods = Interface MethodsImpl = Class

Is there anyway I can do that from a string? Something like this.

String thisMethod = "Method"
String thisMethodImpl = "MethodImpl"

thisMethod meth = new thisMethodImpl();

Something like that? I know its not possible but is there any way how I can do that? Im going to instantiate a dynamic object by the way.

thanks!





jeudi 18 août 2016

Getting all classes in Android project

I am trying to find a way to get a list of all the classes I've made in my Android project, probably using the reflections library.

I've seen other threads on this in the past, but many are years old, and there are occasional comments saying that older methods no longer work based on Gradle upgrades and such.

Older thread here: Find all classes in a package in Android

So I am curious if there is a commonly-accepted or known, definitive approach for how this is done as of 2016.





Is it possible to determine Polymorphism factor or Cyclomatic Complexity through reflection?

Can we determine the polymorphism factor or Cyclomatic Complexity solemnly through the java reflection?If so how?

PF = overrides / sum for each class(new methods * descendants)





Use reflection/generics to generate wrapper class

I'd like to create a wrapper class dynamically, such that for every desired class (probably underneath a certain namespace like DBO) I'll get an appropriate class like this:

public class [ClassName]Wrapper{
    public [ClassName] [ClassName] { get; set; }
}

I'm fairly new to reflection and I've never built a class at runtime. Code to do this would be great, but even pointing out excellent resources to study up on the tools used to do this would be a great move forward for me.

Thanks!





Getting a type representation of a partially open generic type

I am wondering if anyone knows a way to get a representation of a partially open generic type in C#, for example IDictionary<string,>.

What I have tried:

typeof(IDictionary<string,>)

I get: Partially opened type is not permitted in 'typeof' expression compile error.

typeof(IDictionary<,>).MakeGenericType(typeof(string))

I get: ArgumentException (The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation)

Why I want to do this:

I have a number of scenarios where I need to check if a class I have implements an interface, but in several cases I know that some of the generic type parameters have to be specific (i.e. I want something that implements a dictionary with string keys but I don't care about the value type). I realize there are a number of other ways I could do this (for example, by providing an array of necessary generic parameter types to my method). But in looking at the problem I got curious if there is a way to specify partially open / partially closed generic types, hence the question.





Well known Symbols do not work with functions

Consider the following two snippets:

obj = {}; // can be [], Number(), eg. but not Function()
obj[Symbol.hasInstance] = () => console.log('89');
({}) instanceof obj;

and

obj = function() {}; // new Function() too
obj[Symbol.hasInstance] = () => console.log('89');
({}) instanceof obj;

Both should log '89' but only in the first snippet '89' is logged.

In Chrome 52, first logs 89, second logs nothing.

In node v6.3.1 first results in TypeError: Expecting a function in instanceof check, but got #<Object>, while second logs nothing.

Is this a bug? What is the intended behaviour? On MDN there is nothing explaining this.