Tag: 静态

c#:Inherited / interface静态成员?

有没有办法要求一个类有一个特定的抽象成员? 像这样的东西: public interface IMaxLength { public static uint MaxLength { get; } } 或许这个: public abstract class ComplexString { public abstract static uint MaxLength { get; } } 我想强制一种类型(通过inheritance或接口?)具有静态成员的方式。 可以这样做吗?

如何编写正确的静态方法 – multithreading安全

我假设静态方法不应该像第一个片段那样写,或者我错了? public static class ExtensionClass { private static SomeClass object1; private static StringBuilder sb; private static string DoSomething() { sb.AppendLine(object1.SomeValue); } public static string ExtensionMethod(this HtmlHelper helper, SomeClass _object1) { object1 = _object1; sb = new StringBuilder(); DoSomething(); return sb.ToString(); } } 所以我想出了这个: public static class ExtensionClass { private static string DoSomething(ref StringBuilder _sb,SomeClass object1) […]

静态成员和接口

我刚刚对这个主题进行了一些SO问题,发现这是(目前?)不可能在接口中定义静态成员或者使静态方法成为虚拟。 我现在正在努力应对这种情况。 让我试着用一个简单的例子来描述它: 我有一个名为say IAnimal的接口,其定义如下: interface IAnimal { … int Chromosomes {get; } //should ideally be static, but that’s not allowed! … } 然后我有一个实现IAnimal的基类Animal,并提供所有动物共有的一些方法的实现: abstract class Animal : IAnimal { … public abstract int Chromosomes {get; } //necessary becuz of IAnimal … } 在这之下,我有从Doginheritance的Dog , Cat和Zebra类,并为它们的物种提供这个属性的具体实现。 现在问题是:这些类是在运行时发现的(通过Pluggable-modules的Assembly.LoadFile() becuz)。 发现的类保存在List ,我们稍后用它来创建具体的狗和猫。 用于创建动物的输入参数是染色体编号,因此例如他们会问: 创建具有72条染色体的类型的动物。 所以我需要以某种方式获取List中每个Type的Chromosome属性值并返回第一个匹配的Type 。 由于Chromosomes不是静态的(如果是,我可以很容易地运行给定类型的静态成员 […]

有没有办法强制在C#中初始化静态字段?

请考虑以下代码: class Program { static Program() { Program.program1.Value = 5; } static List values = new List(); int value; int Value { get { return value; } set { this.value = value; Program.values.Add(this); } } static Program program1 = new Program { value = 1 }; static Program program2 = new Program { value = […]

在非静态类中调用静态方法时是否实例化了类?

究竟在Bar类中调用Foo.SomeCheck()时会发生什么? 是否为了调用SomeCheck()而创建了一个Foo实例? 如果是这样,这个实例是否存储在堆上,是否通过垃圾收集收集? public class Foo() { public static bool SomeCheck() { return true; } } public class Bar() { public void SomeMethod() { // what happens when we access Foo to call SomeCheck? if (Foo.SomeCheck()) { //do something } } }

C#静态类型不能用作参数

public static void SendEmail(String from, String To, String Subject, String HTML, String AttachmentPath = null, String AttachmentName = null, MediaTypeNames AttachmentType = null) { …. // Add an attachment if required if (AttachmentPath != null) { var ct = new ContentType(MediaTypeNames.Text.Plain); using (var a = new Attachment(AttachmentPath, ct) { Name = AttachmentName, NameEncoding = Encoding.UTF8, […]

绑定到WPF中静态类中的静态属性

我有来自静态类的静态属性的绑定值的问题。 我的课: namespace MyNamespace.Data { public static class MySettings { public static Color BackgroundColor { get; set; } public static Color FontColor { get; set; } } } XAML: … 当我运行此代码后,后台设置正常,但其余部分保持不变..

返回后代的单例实例

我有几个单例类,所以我尝试用方法GetInstance(params)和派生类创建一个父BaseClass,它应该实现这个方法并返回自己的实例(所以我不必抛出它们)… as它们是单例,方法应该是静态的,但不允许覆盖静态方法。 编码它的最佳方法是什么? 我想要的示例代码: public class Base { public static virtual T GetInstance() where T : class; } public class Derived { Derived instance; public static override T GetInstance() where T : typeOf(this){ if (instance == null) { instance = new Derived(); } return instance; } } 在我想要打电话的外面的代码中 Derived.GetInstance().SomeDerivedMethod() 不 (Derived.GetInstance() as Derived).SomeDerivedMethod() and not […]

抽象类不能用c#封装?

我在某处读到了 “抽象和密封修饰符相当于一个静态的类” 我也发现了 “当你声明一个静态类时,编译器在内部标记类抽象并密封,并在IL代码中创建一个私有构造函数” 所以,我决定这样做: static class A { public static void test() { Console.WriteLine(“test”); } } 现在,类“A”不能被inheritance或实例化。 因此,让我们使用abstract编写一个class B来防止实例化并使用sealed来防止inheritance。 但是,这种方法失败了。 这相当于 public abstract sealed class B { private B() { } public void test() { Console.WriteLine(“test”); } } But I recieve an error stating “error CS0418: B’:抽象类不能被密封或静态”。 任何想法为什么这是不可能的? 提前感谢您的回答。

使用静态类/方法依赖项测试类

所以我有一个看起来像这样的类: public class MyClassToTest() { MyStaticClass.DoSomethingThatIsBadForUnitTesting(); } 和一个看起来像这样的静态类: public static class MyStaticClass() { public static void DoSomethingThatIsBadForUnitTesting() { // Hit a database // call services // write to a file // Other things bad for unit testing } } (显然这是一个愚蠢的例子) 所以,我知道第二类在unit testing方面注定要失败,但有没有办法解开MyClassToTest类,以便我可以测试它(没有实例化MyStaticClass )。 基本上,我希望它忽略这个电话。 注意:遗憾的是这是一个Compact Framework项目,所以不能使用像Moles和Typemock Isolator这样的工具:(。