草庐IT

c# - 在类中包装一个简单类型有什么好处?

我正在查看本质上传递id的代码,例如:GetPersonById(intpersonId)但它没有使用int,而是使用了PersonId对象。GetPersonById(PersonIdpersonId)PersonId对象只是一个带有一些手动代码以使其可为空的int。那么,这是在可空整数不可用时在旧.NET中创建的,还是在类中包装简单类型有更高的目的?publicsealedclassPersonId{privatePersonId(){_isNull=true;_value=0;}privatePersonId(intvalue){_isNull=false;_value=valu

c# - 在类中存储不同泛型类型的列表

我附上了我正在尝试做的事情的图片。假设我有一个类(class)中的T列表publicclassMyClasswhereT:IMyInterface{publicListlist=newList;}现在,另一个类有一个MyClass列表。publicclassAnotherClass{publicList>list=newList>;}我应该为MyClass输入什么T?如果我输入T,那么它会假定该类中的所有类型都相同,但事实并非如此。如果我放置IMyInterface,则在访问这些类时无法将IMyInterface强制转换为T。newAnotherClass().list.Add(new

c# - 在类中存储不同泛型类型的列表

我附上了我正在尝试做的事情的图片。假设我有一个类(class)中的T列表publicclassMyClasswhereT:IMyInterface{publicListlist=newList;}现在,另一个类有一个MyClass列表。publicclassAnotherClass{publicList>list=newList>;}我应该为MyClass输入什么T?如果我输入T,那么它会假定该类中的所有类型都相同,但事实并非如此。如果我放置IMyInterface,则在访问这些类时无法将IMyInterface强制转换为T。newAnotherClass().list.Add(new

c# - 以编程方式在类的每个属性上插入方法调用

我的问题是基于thisarticle.基本上一个类可以实现一个Freezable方法来确保一旦对象进入Frozen状态就不能更改任何属性。我有一个遵循这种设计的界面publicinterfaceIFreezableModel{voidFreeze();boolIsFrozen{get;}}目标是确保在调用Freeze方法后,IsFrozen属性设置为True,并且对象的属性不能再更改。为了简化,我将使用一个抽象基类:publicabstractclassBaseFreezableModel:IFreezableModel{publicvoidFreeze(){_isFrozen=tru

c# - 以编程方式在类的每个属性上插入方法调用

我的问题是基于thisarticle.基本上一个类可以实现一个Freezable方法来确保一旦对象进入Frozen状态就不能更改任何属性。我有一个遵循这种设计的界面publicinterfaceIFreezableModel{voidFreeze();boolIsFrozen{get;}}目标是确保在调用Freeze方法后,IsFrozen属性设置为True,并且对象的属性不能再更改。为了简化,我将使用一个抽象基类:publicabstractclassBaseFreezableModel:IFreezableModel{publicvoidFreeze(){_isFrozen=tru

c# - 在类级别获取描述属性

我有这样一个类[Description("Thisisawahalaclass")]publicclassWahala{}有没有办法获取Wahala类的Description属性的内容? 最佳答案 绝对-使用Type.GetCustomAttributes。示例代码:usingSystem;usingSystem.ComponentModel;[Description("Thisisawahalaclass")]publicclassWahala{}publicclassTest{staticvoidMain(){Console.W

c# - 在类级别获取描述属性

我有这样一个类[Description("Thisisawahalaclass")]publicclassWahala{}有没有办法获取Wahala类的Description属性的内容? 最佳答案 绝对-使用Type.GetCustomAttributes。示例代码:usingSystem;usingSystem.ComponentModel;[Description("Thisisawahalaclass")]publicclassWahala{}publicclassTest{staticvoidMain(){Console.W

c# - 在类中打包事件参数,为什么?

大多数.NET股票事件都有这个签名:delegatevoidSomethingSomething(SomethingEventArgse);eventSomethingSomethingOnSomethingSomething;和classSomethingEventArgs{publicstringName;publicintIndex;publicdoubleGroar;}为什么这比(显然是,否则任何人都会选择这样做)更好:delegatevoidSomethingSomething(stringName,intIndex,doubleGroar);eventSomethingSo

c# - 在类中打包事件参数,为什么?

大多数.NET股票事件都有这个签名:delegatevoidSomethingSomething(SomethingEventArgse);eventSomethingSomethingOnSomethingSomething;和classSomethingEventArgs{publicstringName;publicintIndex;publicdoubleGroar;}为什么这比(显然是,否则任何人都会选择这样做)更好:delegatevoidSomethingSomething(stringName,intIndex,doubleGroar);eventSomethingSo

c# - 如何使方法在类中调用另一个方法?

现在我有两个类allmethods.cs和caller.cs。我在allmethods.cs类中有一些方法。我想在caller.cs中编写代码,以便调用allmethods类中的某个方法。代码示例:publicclassallmethodspublicstaticvoidMethod1(){//Method1}publicstaticvoidMethod2(){//Method2}classcaller{publicstaticvoidMain(string[]args){//IwanttowriteacodeheretocallMethod2forexamplefromallmeth