dimanche 22 mars 2015

Getting a reference to "thismember" in C#

Is there a way to get an auto reference to a member function or property in c#?


I mean something like this:



class Foo {
bool prop;
public bool MyProp
{
get { return prop; }
set {
prop = value;
OnPropertyChanged(thismember);
}
}
}


And 'thismember' is something that automatically references the calling property ('MyProp'), of type System.Reflection.PropertyInfo or System.Reflection.MemberInfo?






JAVA getConstructor throws NoSuchMethodException

I'm new in JAVA I'm trying to learn reflection I want to get specific constructor(picking the example form here) from my class :



public class example1 {
public example1() {
}
public example1(int i4) {

}
public example1(String s3) {
System.out.println("using param =" + s3);
}
public String s1;
public String s2;
public int i1;
public int i2;
public Object o1;
public Object o2;

public static void main(String[] args) {

Class<example1> examTesting = example1.class;
String examTestingname = examTesting.getName();
System.out.println("class name =" + (new example1()).getClass().getName());


Class<?> [] paramTypes = String.class.getClasses();
Constructor<example1> ctor = examTesting.getConstructor(paramTypes);

}


}


I get NoSuchMethodException when trying to instantiate ctor


What am I missing here ?






samedi 21 mars 2015

Convert from generic list to specific list using reflection

I'm doing some work with Reflection. My API receives some JSON and I'd like to convert it to a list of the type it is.


Within


public override bool TrySetMember(SetMemberBinder binder, object value)


I have a cached list of properties, and one of them is a List. I know this is true because I can see from prop.PropertyType.


I would like to convert this property to an actual list, of type Foo, but I cannot seem to do this. The best I could get is a List. value is a json array of Foo (so someone sent me JSON in an array [] as Foo).



//this works, and listOfObjects is of type List<Foo> when I go deep into the quick watch of it, but it's still a list<Object> in the end.
var listOfObjects = JsonConvert.DeserializeObject<List<Object>>(value.ToString());


I can directly cast listOfObjects as List and this works, but the problem is Foo can be any type, for example Bar is also possible. So there are infinite possible types here is what i'm saying. Is there a way I can somehow tell it to be Foo when I do a cast? I'm able to get "Foo" type from my property info? I'm always able to find the type I want to convert it to using property info, but then what? I can't do a cast with a variable right?


Thanks!






Mirror reflection quirks in ThreeJS

(Disclaimer: first post on SO. I've searched SO and elsewhere for an answer to this, but while mirrors and reflections do come up, I haven't found anything related to my particular issue.)


Problematic Mirror


This is my first scene in ThreeJS and most of it is based on the official examples (super helpful!). As you can see, for whatever reason the mirror reflection is black/fragmented from most angles, so it only displays a proper reflection under a very narrow range. (Explore other angles to see what I'm talking about.)


Relevant code:



// MIRROR

verticalMirror = new THREE.Mirror( renderer, camera, { clipBias: 0.003, textureWidth: 1024, textureHeight: 1024, color:0x889999 } );

var verticalMirrorMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 300, 300 ), verticalMirror.material );
verticalMirrorMesh.add( verticalMirror );
verticalMirrorMesh.position.y = 100;
verticalMirrorMesh.position.z = -500;
scene.add( verticalMirrorMesh );


And in the render function:



function render()
{
renderer.render( scene, camera );
verticalMirror.renderWithMirror( verticalMirror );
}


I've tried messing with the texture settings and the clipbias to no avail, and the whole code is mostly based on The ThreeJS reference example for Mirror.


Any and all help would be greatly appreciated - thank you!






How to cast a object into another?

First off, if the question is worded wrong. Let me know, I wasn't sure how to word this question. ^^'


Ok, right now, I am working on a plugin API so people can change the programs behavior without having to modify the source, and users who can not program, but still want to modify it, can download other plugins people have made already. And right now, I am currently working on the Event System, where if a event is fired, the plugin can detect that a react to it. And I want the user to be able to put this code:



handleEvent(Event e) {}


And then use:



if(e instanceof <EVENTTYPE>) {
<EVENTTYPE> e2 = (<EVENTTYPE>) e;
// Do code here that could not be normally accessible without casting
}


But I have no idea how to do this, so I have no code to show. But, if you want the code I do have which works fine (Except for this, which I said I have no idea how to do) I can give it to you.






golang reflect, get pointer to a struct field value

I'm trying to make a function that converts a struct in the way mysql rows.Scan function needs it, so I don't need to pass manually lots of parameters.


Note: I know the existence of sqlx and the alternative of writing manually in separate lines every pointer, but I'd like to solve it in this way as I'm learning go and want to understand what's going on.


The error I get with this solution is:

panic: sql: Scan error on column index 0: destination not a pointer to me looks like valueField.Addr().Pointer() should be a Pointer to the value. The following is a simplification of my code.



type User struct {
Name string
Age int
}

func StrutForScan(u interface{}) []interface{} {
val := reflect.ValueOf(u).Elem()
v := make([]interface{}, val.NumField())
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
v[i] = valueField.Addr().Pointer()
}
return v
}

func ListUsers {
rows, err := db.Query("SELECT * FROM users")
PanicIf(err)
var user User
for rows.Next() {
err := rows.Scan(StrutForScan(&user)...)
PanicIf(err)
fmt.Printf("\nName: %s, Age: %s", user.Name, string(user.Age))

}
}





Java - Create instance of a class with String as name

I want to create some instances of class, just like:



Country ger = new Country();
Country usa = new Country();
...


and so on. Because I want to do this for a huge number of objects, I'd rather want to create those instances with instance names from a text file where those country tags are listed and iterate through this list. I am familiar with the Java reflection concept, however I DO NOT WANT to create a single class for each object as I just want to cut those declarations short.


Is there any way to do that?


Thanks in advance