我想从enumD2中的类型中获取名称和值.我知道我可以使用std.traits获取枚举值,但名称呢?
鉴于:
enum lst
{
apple,
bottle,
orange,
blue
}
Run Code Online (Sandbox Code Playgroud)
我想得到一个像关联数组.
string lstmap[int] = [1:"apple", 2:"bottle", 3:"orange", 4:"blue"].
Run Code Online (Sandbox Code Playgroud)
答案是肯定的.有人向我展示的解决方案是:
foreach (i, member; __traits(allMembers, lst)) {
lstmap[cast(int) __traits(getMember, lst, member)] = member;
}
Run Code Online (Sandbox Code Playgroud) 我想列出实现特定特征的所有案例类.我目前正在使用Clapper ClassUtil来做到这一点.我能够获得直接实现特征的案例类.但是,我无法获得其他没有直接实现特征的类.如何获得直接或间接实现特征的所有类.?
val finder = ClassFinder()
finder.getClasses().filter(_.isConcrete).filter(_.implements("com.myapp.MyTrait"))
Run Code Online (Sandbox Code Playgroud)
Scala版本: 2.11
Clapper Class Util版本: 1.0.6
有没有其他方法可以获得这些信息?有人能指出我正确的方向吗?我尝试使用scala.reflect但无法理解如何获取信息.
EDIT:
Run Code Online (Sandbox Code Playgroud)
样本特征和用法:
trait BaseEntity
trait NamedEntity{ val name:String}
trait MasterDataEntity extends NamedEntity
case class Department(id:Long, override val name:String) extends MasterDataEntity
case class Employee(id:Long, name:String) extends BaseEntity
case class User(id:Long, override val name:String) extends NamedEntity
Run Code Online (Sandbox Code Playgroud)
现在,如果我给出特征NamedEntity,我应该能够同时获得两者Department,User因为它们都直接或间接地实现了NamedEntity.使用implements方法,它只会给用户.我也试过使用interfaces方法,它也只提供直接超类.
我希望代码剪切说明我的问题.我需要Invoke的CallEvent方法就像它在out-comment线中一样.我到没有访问ThirdParty或AnotherThirdParty类.
就我而言:
public class ThirdParty
{
private struct MsgType
{ }
private static void AnotherFunc(MsgType msg)
{ }
}
public class AnotherThirdParty
{
public static void CallEvent<T>(Func<int, Action<T>> action, T arg)
{ }
}
public class MyClass
{
public static void Main()
{
Type MsgType = typeof(ThirdParty).GetNestedType(
"MsgType", BindingFlags.Instance | BindingFlags.NonPublic);
object msg = Activator.CreateInstance(MsgType);
MethodInfo CallEvent = typeof(AnotherThirdParty).GetMethod("CallEvent");
CallEvent = CallEvent.MakeGenericMethod(MsgType);
MethodInfo AnotherFunc = typeof(ThirdParty).GetMethod(
"AnotherFunc", BindingFlags.Static | BindingFlags.NonPublic);
CallEvent.Invoke(null, new …Run Code Online (Sandbox Code Playgroud) 无法理解为什么我应该使用Reflect.get(obj, 'foo')而不是obj['foo'],或为什么第一个是有用的,因为我们可以使用旧的和旧的对象括号表示法做同样的事情.有人可以详细说明吗?
var obj = {foo: 'bar'};
obj['foo'];
Reflect.get(obj, 'foo');
Run Code Online (Sandbox Code Playgroud) 如何在getter链之后安全地调用setter,例如foo.getX().getY().setZ(...);?例如,假设我有一个嵌套的POJO,我希望能够设置嵌套对象的字段.
Foo foo = ...
foo.getX().getY().setZ(...);
Run Code Online (Sandbox Code Playgroud)
我希望行为是这样的,如果X和Y不存在,那么它们是自动创建的; 否则它会重用现有对象.
换句话说,我希望它的行为相当于
Foo foo = ...
X x = foo.getX();
if (x == null) {
x = new X();
foo.setX(x);
}
Y y = x.getY();
if (y == null) {
y = newY();
x.setY(y);
}
y.setZ(...);
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一个使用反射/功能的技巧接近这一点.
我也有以下约束:
x = foo.getX()修改foo.我正在使用json4s 3.2.11和Scala 2.11。
我使用定义了一个枚举sealed trait,并为其使用了一个自定义序列化器:
import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString
import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization
sealed trait Foo
case object X extends Foo
case object Y extends Foo
object FooSerializer
extends CustomSerializer[Foo](
_ =>
({
case JString("x") => X
case JString("y") => Y
}, {
case X => JString("x")
case Y => JString("y")
})
)
Run Code Online (Sandbox Code Playgroud)
这很棒,添加到以下格式后效果很好:
{
implicit val formats = DefaultFormats + FooSerializer
Serialization.write(X) // "x"
}
Run Code Online (Sandbox Code Playgroud)
这很棒!
如果未将序列化程序添加到格式中,则json4s将使用反射来创建字段的默认表示形式,这对于这些object没有字段的s 极为不利。它无声地执行此操作,似乎无法控制它。
{
implicit val formats = …Run Code Online (Sandbox Code Playgroud) 在 Kotlin 中,我可以像这样KType从 a 中获得 a KClass<*>:
Int::class.createType()
Run Code Online (Sandbox Code Playgroud)
kotlin.int
我如何做相反的事情并KClass<Int>从KType代表 a 中获得kotlin.Int?
根据https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods 可以使用以下语法显式调用接口基础实现。
base(IInterfaceType).Method();
Run Code Online (Sandbox Code Playgroud)
但这似乎还没有实施。
是否有解决方法(例如反射)来实现这一目标?
示例代码来说明问题
interface IA
{
void M()
{
Console.WriteLine("IA.M");
}
}
interface IB : IA
{
void IA.M()
{
Console.WriteLine("IB.M");
}
}
interface IC : IA
{
void IA.M()
{
Console.WriteLine("IC.M");
}
}
class D : IA, IB, IC
{
public void M()
{
// base(IB).M(); Is not yet supported apparently
((IB)this).M(); // Throws stack overflow
}
}
class Program
{
static void Main(string[] args)
{
D d = new D();
d.M();
}
}
Run Code Online (Sandbox Code Playgroud) 据我所知,静态反射目前在 C++26 的路线图上。
\n反射TS提出了基于类型的语法,但同时也提出了基于值的语法。在P2560 Mat\xc3\xba\xc5\xa1 Chochl\xc4\xb1\xcc\x81k 中,对两种方法进行了比较。
\n是否已经决定哪种方法可能标准化?
\n