【类中的C#调用接口方法】教程文章相关的互联网学习教程文章

c# – 为什么调用这个重写方法?【代码】

public interface ITimeable {} public class TimedDoor : ITimeable {}public static class Timer {public static void Add(ITimeable obj){Console.Write("Add with parameter - ITimeable"); }public static void Add(TimedDoor obj){Console.Write("Add with parameter - TimedDoor"); } }public class BaseClient<T> where T : ITimeable {public T TimedDoorObject;public virtual void Init(){Timer.Add(TimedDoorObje...

c# – 执行COM方法调用的位置【代码】

比方说,我正在执行一个用c#编写的exe(只是我选择的语言).它有以下代码:var comObj=new ComClass(); comObj.DoSomething();现在,我想知道在哪个进程中执行了DoSomething方法.它是当前exe运行或不同进程响应DoSomething调用的进程吗?解决方法:从COM Clients and Servers起There are two main types of servers, in-process and out-of-process. In-process servers are implemented in a dynamic linked library (DLL), and out-of...

C#:具有相同名称和不同签名的多个函数,但编译器调用错误的函数.怎么解决?【代码】

我有两个功能:partial class Database {public void Insert(string table, params string[] values){string query = "INSERT INTO [{0}] VALUES ('{1}')";ExecuteNonQuery(string.Format(query, table, string.Join("','", values)));}public string Insert(string table, string returnedColumn, params string[] values){string query = "INSERT INTO [{0}] OUTPUT INSERTED.{1} VALUES ('{2}')";return ExecuteScalar(string.F...

c# – 未调用构造函数【代码】

public class Unit {public int UnitId { get; set; }public Engine EngineStuff { get; set; }} public class Engine {public int PS { get; set; }public int MaxSpeed { get; set; } }var unit = new Unit(); unit.UnitId = 3; //OK because Unit-constructor was called unit.EngineStuff.PS = 200; //error, because EngineStuff-constructor obviously wasn't called.如何调用这个“内部”构造函数?我以为它会自动初始化它?...

c# – 为什么我必须在泛型类上使用静态方法调用【代码】

我遵循this泛型类的例子.因为我不想用测试代码填充我的项目的主要功能,所以我想创建一个运行代码示例的静态展示函数. 我的代码:namespace Syntax {public class GenericClass<T>{private class Node{private T data;private Node next;public Node(T t){next = null;data = t;}public Node Next { get { return next; } set { next = value; } }public T Data { get { return data; } set { data = value; } }}private Node head;...

让当前线程在让其他线程调用C#之前使用该函数完成【代码】

我似乎无法获得thread.Join()来为这个例子工作.我是多线程和C#的新手,所以我不确定如何将在线示例应用到我自己的编码中.这是我编码的一个例子:using System; using System.Threading;public class Example {static Thread thread1, thread2, thread3, thread4;public static void Main(){Console.WriteLine("Starting thread1");thread1 = new Thread(ThreadProc);thread1.Name = "Thread1";thread1.Start();Console.WriteLine("S...

c# – 基础构造函数调用的静态是什么?【代码】

参见英文答案 > Cannot access non-static field 2个以下C#代码无法编译.public class BaseType {public BaseType(int bar){// Do stuff with bar...} }public class DerivedType : BaseType {private int foo;public DerivedType() : base(foo = 0) {} }在调用DerivedType的基础构造函数时发生错误,消息“无法在静态上下文中访问非静态字段’foo’.”这个错误消息告诉我什么? ‘foo’不是静态...

c# – 使用Task.Run()创建的任务调用await【代码】

为什么可以在C#中执行此操作?var task = Task.Run (...); await task;不应该将Task.Run()用于CPU绑定代码吗?呼叫等待这个有意义吗? 即,在调用Task.Run之后,我理解该任务正在线程池的另一个线程中运行.呼叫等待的目的是什么?调用task.Wait()会更有意义吗? 最后一个问题,我的第一印象是await旨在专门用于异步方法.将它用于Task.Run()返回的任务是否常见? 编辑.它也让我想知道,为什么我们有Task.Wait()而不是Task.Await().我的意...

c# – 如何在.NET MVC中以特定的时间间隔调用方法

我在MVC 3和C#中有一个Web应用程序. 我需要每隔1小时运行一次方法,无限时间.我想知道如何实现它. Thansk. 有关 Call MVC Controller from Windows task scheduler解决方法:您可以使用System.Timers.Timer.但请注意implementing recurring background tasks in ASP.NET applications是一项危险的任务.不要忘记IIS可以随时回收应用程序池,并且在某些情况下(站点上处于非活动状态,达到CPU /内存阈值,……),可以降低您可能已启动的所有...

c# – 我应该在ServiceController上调用Close()吗?【代码】

目前我有这样的方法:private bool IsMyServiceRunning(string serviceName){if (String.IsNullOrEmpty(serviceName))throw new InvalidOperationException("ServiceName cannot be null or empty");using (var service = new ServiceController(serviceName)){if (service.Status == ServiceControllerStatus.Running)return true;elsereturn false;}}这是使用ServiceController类的正确方法吗? 我问的原因是我看到的所有示例都没...

c# – 在构造函数中委派调用【代码】

我遇到了一个容易解决的设计问题,但是在我年轻的时候还没有遇到过. 我有一个课程需要经过一些设置程序才能发生其他事情. 但是,在构造这个类的过程中,我在构造函数的参数中有一个可以传递的委托,以便用户可以将自己的信息添加到类中. 当调用它时,创建类的作用域仍然没有有效的实例,因此会发生null异常错误. 我该如何设计呢?我应该将“this”的实例传递给该代表吗? 在这里做出什么样的好决定?我有一个“StartServices()”方法,我可...

如何在不创建实例的情况下从C#类调用静态方法【代码】

我有这样的代码:class Program{static void Main(string[] args){Assembly myAsm = Assembly.LoadFile(@"c:\Some.dll");Type myService = myAsm.GetType("SomeClass");String s = (String) myService.InvokeMember("SomeMethod", BindingFlags.InvokeMethod | BindingFlags.Public,null, null, new object[] {"MyParam"});}}在Some.Dll中有公共静态方法SomeMethod和String param返回String但我得到方法缺少错误…解决方法:您缺少...

c# – 来自浏览器的WCF方法调用返回400错误请求【代码】

我在Visual Studio 2008中使用ASP.NET 3.5和C#.我对WCF很天真.我已经创建了一个示例wcf应用程序,并在Visual Studio中的asp.net开发服务器上运行它.我正在调用GetData()方法,即在创建应用程序并获得400个错误请求时自动生成.我已经尝试了很多,但结果令人沮丧. 我的界面IService1.cs如下:using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; usin...

c# – 编辑界面时调用了什么?【代码】

我正在浏览LitJSON库.在代码中有很多段,如public class JsonData : IJsonWrapper, IEquatable<JsonData>#region ICollection Propertiesint ICollection.Count {get {return Count;}}#end region对于我知道覆盖/重载如何工作的方法,但在上面的示例中,代码为:int ICollection.Count 我不熟悉方法签名的格式.编码器是否试图明确声明其ICollection.Count接口? 你能解释一下这是什么“被称为”(它是否仍然覆盖?).解决方法:它被称为e...

调用基础构造函数c#时出错【代码】

class Student {int id;string name;public Student(int id, string name){this.id = id;this.name = name;}public int Id{get { return id; }set { id = value; }}public string Name{get { return name; }set { name = value; }} } class SubStudent : Student {int ssn;public SubStudent(int id, int name, int ssn): base(int id, string name){} }上面的代码生成错误“term int的无效表达式”可能有什么不对?解决方法:您不需...