在C++中,我曾经写过一个函数,它通过传递给它的float参数的数量来停止程序的执行,这里有一个函数:
void wait(float sec){
clock_t endwait = clock() + sec * CLOCKS_PER_SEC; // CLOCK_PER_SEC == 1000
while(clock() < endwait);
}
Run Code Online (Sandbox Code Playgroud)
C#中有一个等效的内置方法吗?如果没有,我怎么能在C#中写一个这样的?
Thnx提前.. :)
我对此感到非常沮丧..
我有一个调用的计时器timer1和一个调用的文本框TimeElapsedTextBox,一个double名为TimeTakenToFinish
timer 的变量在文本框中每1秒(1000毫秒)滴答一次,我希望它以这种格式显示时间:
Seconds.PartsOfSecond
Run Code Online (Sandbox Code Playgroud)
这是Tick事件:
private void timer1_Tick(object sender, EventArgs e)
{
TimeTakenToFinish += (double)timer1.Interval / 10000;
TimeElapsedTextBox.Text = TimeTakenToFinish;
}
Run Code Online (Sandbox Code Playgroud)
它实际上是按照我想要的方式在文本框中显示它,但它没有正确计算...我的意思是,它的计数不到真正的第二个..
你能告诉我如何解决这个问题吗?
让我们说以下类型:
public class Object { }
public class GameObject : Object { }
public class Component : Object { }
public class Transform : Component { }
Run Code Online (Sandbox Code Playgroud)
还有两个 Type[]
var t1 = new [] { typeof(Object), typeof(Component) };
var t2 = new [] { typeof(GameObject), typeof(Transform) };
Run Code Online (Sandbox Code Playgroud)
和扩展方法:
Console.Writeline(t1.IsAssignableFrom(t2); // prints true
Run Code Online (Sandbox Code Playgroud)
我以一种基本的方式编写了这个方法:
public static bool IsAssignableFrom(this Type[] to, Type[] from)
{
if (to.Length != from.Length) return false;
for (int i = 0; i < to.Length; i++) {
if (!to[i].IsAssignableFrom(from[i])) …Run Code Online (Sandbox Code Playgroud) 基本上,我有:
public class Gen<T, U> { }
Run Code Online (Sandbox Code Playgroud)
我想通过该类的反射创建一个泛型类型.我尝试了以下,都产生了空值:
var asm = typeof(Program).Assembly;
var gen1 = asm.GetType("Gen`2"); // null
var gen2 = asm.GetType("Gen<>"); // null
var gen3 = asm.GetType("Gen"); // null
Run Code Online (Sandbox Code Playgroud)
这没有编译:
var t = typeof(Gen<>).MakeGenericType(typeof(int), typeof(string));
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现我的目标?为什么最后一个语句没有编译?那么"Gen`2"应该不会起作用吗?