reflection - TypeScript:在运行时获取类名?

标签 reflection typescript

<分区>

在 C# 中,使用反射很容易在运行时获取类名。 在 TypeScript 中有可能吗?

最佳答案

在运行时,您正在运行 JavaScript。所以你可以检查这个answer了解详情。

Here is a hack that will do what you need - be aware that it modifies the Object's prototype, something people frown upon (usually for good reason)

Object.prototype.getName = function() { 
   var funcNameRegex = /function (.{1,})\(/;
   var results = (funcNameRegex).exec((this).constructor.toString());
   return (results && results.length > 1) ? results[1] : "";
};

Now, all of your objects will have the function, getName(), that will return the name of the constructor as a string. I have tested this in FF3 and IE7, I can't speak for other implementations.

关于reflection - TypeScript:在运行时获取类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22777181/

相关文章:

javascript - 观看模板文件并将它们复制到 dist/文件夹

java - 是否可以获得该对象的名称?

.net - MethodBuilder.DefineParameter 无法设置参数名称的原因?

angular - 我如何控制台记录订阅中的结果?

javascript - 如果总是以不同/随机名称返回,如何绑定(bind)到 json key ?

angular - 是否有最佳实践来放置跨文件共享的全局变量?

javascript - 从函数体参数获取值

java - 获取可从 java 源代码调用的类中的方法列表

c# - 在构造函数上调用 MethodBase 的 Invoke(反射)

如果尝试使用反射将值设置为 String 类的 value[] 字段,Java 6 和 Java 7 会产生不同的结果