所以我使用以下实用程序从类的实例中获取字段/属性的名称...
public static string FieldName<T>(Expression<Func<T>> Source)
{
return ((MemberExpression)Source.Body).Member.Name;
}
Run Code Online (Sandbox Code Playgroud)
这允许我执行以下操作:
public class CoolCat
{
public string KaratePower;
}
public class Program
{
public static Main()
{
public CoolCat Jimmy = new CoolCat();
string JimmysKaratePowerField = FieldName(() => Jimmy.KaratePower);
}
}
Run Code Online (Sandbox Code Playgroud)
当我需要字段名称的字符串表示时,这非常适合序列化和其他时间.
但是现在,我希望能够获得字段名称而没有类的实例 - 例如,如果我正在设置一个表并希望将列的FieldNames动态链接到类中的实际字段(因此重构)等等不会破坏它).
基本上,我觉得我还没有完全掌握如何实现这一点的语法,但我想它会看起来像这样:
public static string ClassFieldName<T>(Func<T> PropertyFunction)
{
// Do something to get the field name? I'm not sure whether 'Func' is the right thing here - but I would imagine that it is something where …Run Code Online (Sandbox Code Playgroud) 我的Java应用程序需要在远程调用失败时使用重试逻辑.这些远程呼叫是:
而且,重试逻辑可以具有变化的重试间隔和变化的重试尝试.
我需要一个通用的retry()实现,它可以根据调用的位置进行适当的方法调用.下面是我正在寻找的简单代码示例.我知道我们可以尝试使用java反射来实现这一点,但是,是否有一个可以读取使用的框架或开源?
try {
ClassA objA = remoteServiceA.call(paramA1, paramA2, ...);
} catch (Exception e){
ClassA objA = (ClassA)retry(remoteService, listOfParams, ..); // generic method call
}
..
try {
ClassB objB = remoteServiceB.call(paramB1, paramB2, ...);
} catch (Exception e){
ClassA objB = (ClassB)retry(remoteService, listOfParams, ..); // generic method call
}
Run Code Online (Sandbox Code Playgroud) 我有一个对象Field field.
我想检查是否field是类型的对象Foo或数组:Foo[].
Psuedo代码:
if field.getType() is Foo || field.getType is Foo[]
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我试过了
if (field.getType().isArray())
// do something
Run Code Online (Sandbox Code Playgroud)
但这只允许我检查是否field是一个数组.
相反,这样做只会检查它是否是一个对象 Foo
if (Foo.class.isAssignableFrom(field.getType())
// do something
Run Code Online (Sandbox Code Playgroud)
知道怎么做吗?
谢谢.
想象一下以下场景:
class MyClass extends OtherClass<String>{
String myName;
//Whatever
}
class OtherClass<T> {
T myfield;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用特定的反射分析MyClass (MyClass.class).getDeclaredFields(),在这种情况下,我将得到以下字段(和类型,使用字段的getType()):
myName --> String
myField --> T
Run Code Online (Sandbox Code Playgroud)
我想获得T的实际类型,由于扩展符号中的显式"字符串",它在运行时已知,如何获取myField的非遗传类型?
编辑决议:
似乎答案是"你不能".对于那些以后可能会看到这个问题的人,我建议使用Jackson(我试图这样做来生成JSON)并以这样的方式注释你的类和字段,以便杰克逊知道继承层次结构并且可以自动做什么建议如下正确答案.
我的任务是找到案例类的参数名称,其中有默认值.
在2.9我从scalap使用ScalaSigParser并做了类似的事情:
(...)
case x: MethodSymbol if x.name.startsWith("init$default$") => (...)
(...)
Run Code Online (Sandbox Code Playgroud)
我希望2.10中的反思会让我更容易获得这种信息.
最后我想编写一个宏,我将在case类的伴随对象中包含它,它将自动创建一个用于该case类的序列化/反序列化的代码.为此,我需要知道哪些参数具有默认值.
namespace foo;
class a
{
private $bar;
public $baz;
protected $alpha
}
$reflect=new \ReflectionClass('a');
$properties=$reflect->getProperties(ReflectionProperty::IS_PROTECTED);
Run Code Online (Sandbox Code Playgroud)
它将返回ReflectionProperty未找到的类致命错误其中$ properties是对象的数组ReflectionProperty.为什么它不能自动解决全球空间?与DOM相关的类隐含地执行此操作.如果ReflectionPropertyclass use在命名空间中是d,那么它可以工作.但为什么不隐含地发生呢?
我有以下代码用于处理我的Web应用程序中的异常:
application.Response.Clear();
application.Response.Status = Constants.HttpServerError;
application.Response.TrySkipIisCustomErrors = true;
LogApplicationException(application.Response, exception);
try
{
WriteViewResponse(exception);
}
// now we're in trouble. lets be as graceful as possible.
catch (Exception exceptionWritingView)
{
// Exception wrapper = ??
WriteGracefulResponse(exceptionWritingView);
}
finally
{
application.Server.ClearError();
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是,如果在尝试将响应呈现为a时出现异常ViewResult,我将"抑制"原始异常(View无论如何),并仅显示导致错误ViewResult抛出的原因.
我想要两者exception并且两者都exceptionWritingView做一个例外,exceptionWritingView在顶部.
这将是:
exceptionWritingView
-> inner exceptions of exceptionWritingView
-> original exception that raised the unhandled exception handler
-> its inner exceptions
Run Code Online (Sandbox Code Playgroud)
但我不能InnerException在我的exception对象上设置属性.那我该怎么做呢?
在"最佳"我可以创建一个新的 …
请考虑以下代码:
object ResponseType extends Enumeration {
val Listing, Album = Value
}
Run Code Online (Sandbox Code Playgroud)
我可以Symbol像这样引用这个对象:
import reflect.runtime.universe._
val s = typeOf[ResponseType.Value].asInstanceOf[TypeRef].pre.typeSymbol
Run Code Online (Sandbox Code Playgroud)
现在,有了这个符号,我怎样才能获得实际的ResponseType对象?
有以下枚举
object ResponseType extends Enumeration {
val Listing, Album = Value
}
Run Code Online (Sandbox Code Playgroud)
我如何获得其vals列表?
在scala 2.9.x中,我tools.nsc.Global直接将某些字符串编译成一个类并执行它.
在scala 2.10中,可以用以下内容替换它:
import scala.reflect.runtime._;
val cm = universe.runtimeMirror(getClass.getClassLoader)
import scala.tools.reflect.ToolBox;
val tb = cm.mkToolBox()
tb.eval(tb.parse("class C; new C"))
Run Code Online (Sandbox Code Playgroud)
它完美无瑕.唯一的问题是,使用旧的(不推荐的)方法,我可以使用StoreReporter(带有错误消息,行号)获得所有编译失败的非常漂亮的摘要,并且新方法只会在编译错误时引发异常.
有什么方法可以实现这一点吗?
reflection ×10
scala ×4
java ×3
scala-2.10 ×3
c# ×2
enumeration ×1
exception ×1
field ×1
frameworks ×1
func ×1
generics ×1
lambda ×1
macros ×1
namespaces ×1
php ×1
toolbox ×1
types ×1