我有一片,interface{}我需要检查这个片是否包含指针字段值.
澄清示例:
var str *string
s := "foo"
str = &s
var parms = []interface{}{"a",1233,"b",str}
index := getPointerIndex(parms)
fmt.Println(index) // should print 3
Run Code Online (Sandbox Code Playgroud) 继例子这篇文章和它的后续问题,我试图创建一个使用编译表达式场getter/setter方法.
getter工作得很好,但是我被困在了setter中,因为我需要setter来分配任何类型的字段.
在这里我的setter-action builder:
public static Action<T1, T2> GetFieldSetter<T1, T2>(this FieldInfo fieldInfo) {
if (typeof(T1) != fieldInfo.DeclaringType && !typeof(T1).IsSubclassOf(fieldInfo.DeclaringType)) {
throw new ArgumentException();
}
ParameterExpression targetExp = Expression.Parameter(typeof(T1), "target");
ParameterExpression valueExp = Expression.Parameter(typeof(T2), "value");
//
// Expression.Property can be used here as well
MemberExpression fieldExp = Expression.Field(targetExp, fieldInfo);
BinaryExpression assignExp = Expression.Assign(fieldExp, valueExp);
//
return Expression.Lambda<Action<T1, T2>> (assignExp, targetExp, valueExp).Compile();
}
Run Code Online (Sandbox Code Playgroud)
现在,我将通用setter存储到缓存列表中(当然,每次构建setter都是性能杀手),我将它们转换为简单的"对象":
// initialization of the setters dictionary
Dictionary<string, object> setters = new Dictionary(string, object)();
Dictionary<string, …Run Code Online (Sandbox Code Playgroud) 我在intellij中有这个代码:
return collection.stream().anyMatch(annotation ->
method.isAnnotationPresent(annotation));
Run Code Online (Sandbox Code Playgroud)
并且编译器告诉我"method.isAnnotationPresent(annotation)"可以用方法引用替换,我无法弄清楚如何做,因为它有一个参数.
有谁知道怎么做?
新的反射API引入了TypeInfo类:https:
//msdn.microsoft.com/en-us/library/system.reflection.typeinfo(v = vs.110).aspx
我可以通过写作获得一个(例如,a )的TypeInfo实例TypeCar
TypeInfo typeInfo = typeof(Car).GetTypeInfo();
Run Code Online (Sandbox Code Playgroud)
现在,如果我只有一个TypeInfo实例,我如何得到Type它的引用呢?我可以写吗
Type type = typeInfo.GetType();
Run Code Online (Sandbox Code Playgroud)
或者这将返回一个type等于typeof(TypeInfo)?
我正在尝试为私有字段的setter获取lambda方法句柄,但由于某种原因,无法找到setter.
这是我正在使用的功能界面:
@FunctionalInterface
public interface ISetter<T, R> {
void set(T object, R value);
}
Run Code Online (Sandbox Code Playgroud)
这是用于获取setter的方法:
public ISetter getSetter(Class clazz, String fieldName, Class fieldType) throws Throwable {
MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType setter = MethodType.methodType(Void.class, fieldType);
MethodHandle target = caller.findVirtual(clazz, computeSetterName(fieldName), setter);
MethodType func = target.type();
CallSite site = LambdaMetafactory.metafactory(
caller,
"set",
MethodType.methodType(ISetter.class),
func.generic(),
target,
func
);
MethodHandle factory = site.getTarget();
ISetter r = (ISetter) factory.invoke();
return r;
}
Run Code Online (Sandbox Code Playgroud)
按照惯例,所有的setter都是相同的:"setField(...)".这是我的测试类:
public class TestEntity {
private Long id;
public TestEntity(Long id) { …Run Code Online (Sandbox Code Playgroud) @ComponentScan将为您提供@Component包含(或@Service/ @Repository)注释的所有类的列表.为此,我想他们使用反射来枚举包中的所有类,并找到具有该注释的类.
但是,根据其他StackOverflow答案,由于ClassLoader工作原理,您无法可靠地枚举包中的所有类.那么@ComponentScan看似如何设法实现这一目标呢?
我有一个REST端点和我想的UI来传递他们希望通过自己的排序结果中的字段名称"id","name"等我想出了下面,但真的想使用反射/泛型所以这可能是扩大到包括每我的项目中的对象.
如果我想为100个不同的类提供相同的功能,我觉得这个解决方案不易维护.
public static void sort(List<MovieDTO> collection, String field) {
if(collection == null || collection.size() < 1 || field == null || field.isEmpty()) {
return;
}
switch(field.trim().toLowerCase()) {
case "id":
collection.sort(Comparator.comparing(MovieDTO::getId));
break;
case "name":
collection.sort(Comparator.comparing(MovieDTO::getName));
break;
case "year":
collection.sort(Comparator.comparing(MovieDTO::getYear));
break;
case "rating":
collection.sort(Comparator.comparing(MovieDTO::getRating));
break;
default:
collection.sort(Comparator.comparing(MovieDTO::getId));
break;
}
}
Run Code Online (Sandbox Code Playgroud)
关于如何更好地实现这一点的任何想法,以便它可以扩展到几乎没有维护的企业应用程序?
在Matlab中,您可以使用以下语法忽略输出:
[~, ixMax] = max(foo);
Run Code Online (Sandbox Code Playgroud)
我有一个功能,有签名
[out, out1, out2, out3] = function foo(in1, in2, in3)
Run Code Online (Sandbox Code Playgroud)
out1,out2并且out3是可选输出,每个仅在非常特定(不寻常)的情况下需要. Foo在计算上是昂贵的,并且out1/ out2或者out3都在计算上更加昂贵,但依赖于由中间生成的中间状态foo.我希望能够避免计算out1/ out2/ out3如果调用者使用a ~来忽略它们.我如何在定义中检查foo?
我从PEP 3131中了解到Python中支持非ASCII标识符,尽管它不被认为是最佳实践.
但是,我得到了这种奇怪的行为,我的 identifier (U+1D70F) seems to be automatically converted to ?(U + 03C4).
class Base(object):
def __init__(self):
self. = 5 # defined with U+1D70F
a = Base()
print(a.) # 5 # (U+1D70F)
print(a.?) # 5 as well # (U+03C4) ? another way to access it?
d = a.__dict__ # {'?': 5} # (U+03C4) ? seems converted
print(d['?']) # 5 # (U+03C4) ? consistent with the conversion
print(d['']) # KeyError: '' # (U+1D70F) ?! unexpected!
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?为什么会发生这种静默转换?NFKC …
假设我有一个通用接口 IService<T>和一个实现它的类Service : IService<Bar>
我创建了该接口的代理:
var proxy = new DynamicProxy<IService<Bar>>(new Service()).GetTransparentProxy() as IService<Bar>;
Run Code Online (Sandbox Code Playgroud)
DynamicProxy是RealProxy的一个简单实现:
public class DynamicProxy<I> : RealProxy
{
private I _decorated;
public DynamicProxy(I decorated) : base(typeof(I))
{
this._decorated = decorated;
}
public override IMessage Invoke(IMessage msg)
{
IMethodCallMessage methodCall = (IMethodCallMessage)msg;
MethodInfo methodInfo = methodCall.MethodBase as MethodInfo;
return new ReturnMessage(
methodInfo.Invoke(this._decorated, methodCall.InArgs),
null,
0,
methodCall.LogicalCallContext,
methodCall);
}
}
Run Code Online (Sandbox Code Playgroud)
直接使用我的代理时它工作正常:
IEnumerable<Bar> bars = new List<Bar>() { new Bar { id = 2 }, new Bar { id …Run Code Online (Sandbox Code Playgroud) reflection ×10
java ×4
c# ×3
java-8 ×3
.net-core ×1
annotations ×1
comparator ×1
fieldinfo ×1
function ×1
generics ×1
go ×1
go-interface ×1
identifier ×1
lambda ×1
matlab ×1
pointers ×1
proxy ×1
python ×1
remoting ×1
setter ×1
sorting ×1
spring ×1
unicode ×1