reflection

.NET reflection: how to get properties defined on partial class

旧时模样 提交于 2020-01-03 17:47:10
问题 I use the .NET Entity Framework. I want to copy properties from one EntityObject to another. But System.Type.GetProperties() does not seem to return the properties defined on the partial class. Code: In XXX.edmx/ XXX.Designer.cs generated by Visual Studio, I have class MyTable: public partial class MyTable: EntityObject{..} I want to add some properties to MyTable class, so I add file XXX.Manual.cs: public partial class MyTable: EntityObject{ public string myProp{get;set;} } But myTableObj

Isn't accessing private fields and properties due to reflection a security issue?

纵饮孤独 提交于 2020-01-03 17:37:08
问题 I just recently found out here that it is possible (at least in c#) to look up private fields and properties due to reflection. I was surprised, although I knew that somehow constructs like the DataContractSerializer class need the possibility to access them. The question now is, if anyone can access every field in my classes, this is kind of insecure, isn't it? I mean what if someone has a private bool _isLicensed field. It could be changed easily! Later I found out here that the field

java.lang.NoSuchFieldException: when using reflection

感情迁移 提交于 2020-01-03 17:30:50
问题 public static <A, B> B convert(A instance, Class<B> targetClass) throws Exception { B target = (B)targetClass.newInstance(); for (Field targetField : targetClass.getDeclaredFields()) { targetField.setAccessible(true); Field field = instance.getClass().getDeclaredField(targetField.getName()); field.setAccessible(true); targetField.set(target, field.get(instance)); } return target; } Above is the code I get from forum, When I try to reflect an single type object it works, but when I try on the

Getting a java.lang.Class with generics

给你一囗甜甜゛ 提交于 2020-01-03 17:20:11
问题 I'm a C# guy learning Java and I'm trying to understand how generics work in Java. Given a class or interface "SomeThing", I know I can do this to get the Class of that type: Something.class Now, given a generic interface I'd love to write (GenericInterface<SomeClass>).class but the compiler doesn't like that much. Thanks for any help. 回答1: That's because, thanks to erasure, there isn't a GenericInterface<SomeClass> class. There's just a GenericInterface class, which has a generic parameter.

Extract data from .NET code

被刻印的时光 ゝ 提交于 2020-01-03 17:13:12
问题 TLDR; I need to extract the string value of the parameter of all calls to the index operator of Microsoft.Extensions.Localization.IStringLocalizer in my .NET code. Actually I also need the full name of the enclosing class. Caveat: The above also includes my ASP.NET Core MVC razor views. Basically my need is quite similar to the "Find all references" functionality in Visual Studio. Why and other thoughts The reason is that we appreciate the development flow using IStringLocalizer without the

How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?

左心房为你撑大大i 提交于 2020-01-03 17:09:45
问题 I am writing client code in Scala that needs to interface with a framework in Java. The framework is responsible for creating object instances of classes specified via an API, which it does using reflection. For example: public class ReflectionUtil { public static <T> T newInstance(Class<T> aClass) { T result; try { Constructor<T> meth = aClass.getDeclaredConstructor(new Class[]{}); meth.setAccessible(true); result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); }

Go reflection with interface embedded in struct - how to detect “real” functions?

◇◆丶佛笑我妖孽 提交于 2020-01-03 13:59:58
问题 The situation I have now is the same as was asked about in this thread: Meaning of a struct with embedded anonymous interface? type A interface { Foo() string } type B struct { A bar string } Idiomatically, coming from a backround in OOP languages, what it looks like this pattern is "trying to say" to me is that B must implement interface A. But I get by now that "Go is different". So, rather than the compile-time check I expected at first, this is happy to compile with or without a func (B)

Replace Property Setter method at runtime

岁酱吖の 提交于 2020-01-03 13:33:47
问题 I have a number of objects that all share a common base class, I wish to intercept all calls that set Property values and record if these have been set on a per instance basis. Can I replace the Set Method of a Property at runtime with Reflection? 回答1: One approach there is to make the property virtual, and at runtime create a subclass via reflection-emit that override the properties, adding your code. However, this is advanced, and requires you always make sure to create the subclass (so no

Polymorphic copy in Java

大憨熊 提交于 2020-01-03 13:20:09
问题 I've suddenly encountered a problem of making a deep polymorphic copy in Java. Implementing Clonable solves the problem in my case, but it is often referred as a "bad" technique. So, here are my attempts to find a "no-Clonable" solution: public class Parent { int x; public Parent() {} public Parent(int x0) { x = x0; } public Parent copy() { Parent b = new Parent(); b.assign(this); return b; } protected void assign(Parent c) { x = c.x; } @Override public String toString() { return getClass()

How to inject EasyMock mock into tested class private field

雨燕双飞 提交于 2020-01-03 13:16:08
问题 I'm using EasyMock to create mock that is one of private parameters (without setter) in tested class. I tried using reflection - but it does not work correctly. public class TestedClassTest{ @Test public void test(){ TestedClass instance = new TestedClass(); MockedClass mocked = EasyMock.createMock(MockedClass.class); Data data = new Data(); //Void setter DataType dataType = (myDataType.DataType) EasyMock.anyObject(); mocked.setDataType(dataType); EasyMock.expectLastCall(); //expect EasyMock