<分区>
在 C# 中,使用反射很容易在运行时获取类名。 在 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/