【c# – 区分父类对象中的子类?】教程文章相关的互联网学习教程文章

C#如何在派生类中不显示父类的一些属性以及TypeDescriptor使用【代码】

public SonClass:FatherClass{定义属性。。。。}Type thisType = typeof(SonClass);方法一:PropertyInfo[] pis = thisType.BaseType.GetProperties();//thisType.BaseType就是FatherClassforeach (PropertyInfo p in pps){properties.Remove(p.Name);}方法二:PropertyInfo[] pis = thisType.GetProperties();foreach (PropertyInfo p in pps){if (p.DeclaringType != thisType)properties.Remove(p.Name);}} 方法三:/// <summa...

C#中,子类构造函数调用父类父类构造函数的正确方式【图】

正确调用的方式是: 原文:https://www.cnblogs.com/lz19980926/p/9013091.html

C# 派生无无参构造构造方法的父类【代码】

当一个类派生自无无参构造方法的父类时,子类需要先调用父类的相关构造方法,以便初始化父类的成员using System; using System.Collections.Generic; using System.Text;namespace DailyCoding.Code {// 父类无无参构造方法public class _20210424_1{public void Run(){var r = new _20210424_1_C();}}public class _20210424_1_P{public string _x;public _20210424_1_P(string x) => _x = x;}public class _20210424_1_C : _2021...

c#-将父类动态转换为多个子类之一【代码】

因此,我对编程还不陌生,因此我试图查看是否有可能编写一种方法来检查父类的类型以找到其类型,然后针对任何结果执行相同的代码块.基本上,我只是想看看是否有一种方法可以避免在有多个不同的子类时避免长if,else if语句. 例如代替public Class Shape public Class Circle : Shape public Class Rectangle : Shape public Class Polygon : Shape ....Shape shape;if(shape.GetType() == typeof(Rectangle)) {var asRectangle = (Recta...

C#无法从父类访问Form的公共成员【代码】

我试图在一个类中添加一个事件处理程序,以引用在该类中实例化的窗体控件的事件.所有类都存在于同一名称空间中. 该程序基于ApplicationContext窗体应用程序.在Program.cs的静态void Main()中CustomApplicationContext applicationContext = new CustomApplicationContext(); Application.Run(applicationContext);在公共类CustomApplicationContext中public class CustomApplicationContext : ApplicationContext {//create the app...

c#-将派生类转换为表达式中的父类【代码】

我有一个基类(BankAccount),然后它具有UkBankAccount的派生类型. 我正在获取子类的字段列表作为表达式:var properties = new Expression<Func<UkBankAccount, object>>[] {x => x.Status,x => x.Name,x => x.SortCode,x => x.AccountNumber };但是,然后要将其强制转换为BankAccount类型,以便可以通用地使用它.我试过了:updatedProperties = properties.ToList().Cast<Expression<Func<BankAccount, object>>>().ToArray();但这会...

c#-为父类提供静态变量【代码】

我想使用用于管理数据库表的工具来上课.但是我需要以某种方式将表名从数据类发送回工具类.我设法在非静态环境中完成这项工作,但是我也需要在静态函数中进行这项工作. 我做了一些谷歌搜索,但没有发现任何帮助. 用法示例: 呼叫者User.Delete(1);工具类public class DBTools {public static string table_name = "NULL"; public static void Delete(int id){Console.WriteLine(table_name);}... }资料类别public class User : DBTool...

嵌套类无法在C#中调用父类,但可以在Java中调用【代码】

如果我有一个嵌套类的父类.然后在Java中,嵌套类可以在其父类中调用方法.但是在C#中您无法做到这一点,并且抱怨说“无法调用非静态的外部类方法”. 行为不同的任何原因是什么? PS:我是一名Java程序员,正在学习C#,请原谅我有限的知识 例:public class parent{void methodA(){ }public class nested{void methodB(){methodA(); /// <-- not allowed in C# but good in java} } }解决方法:这是因为在Java中,我们有inner classes...

c# – 我们什么时候真的需要在重写方法中调用父类的重写方法?【代码】

我们什么时候真的需要从子代的覆盖方法中调用父代的重写方法?namespace MvcMovie.Models {public class MovieInitializer: DropCreateDatabaseIfModelChanges<MovieDbContext>{protected override void Seed(MovieDbContext context){base.Seed(context);// is it necessary to invoke this parent's method here?}} }解决方法:直言不讳的答案是 – “你永远不知道何时调用基类方法”. 您还可以质疑调用基类方法的顺序?在派生类实...

获取null对象的父类(C#Reflection)【代码】

如何获取值为null的对象的父类? 例如… ClassA包含int? i在创建类时未设置为任何值. 然后在代码中的其他一些地方我希望将i作为参数传递给某个函数.使用i作为唯一的信息,我希望能够弄清楚ClassA“拥有”我. 原因是因为ClassA还包含一些其他对象,我想从上一段中提到的相同函数中调用另一个对象的值. 也可能是:public class A {public class B{public int? i;public int? j;}B classBInstance = new B();public string s; }{...A s...

c# – 需要根据StructureMap中请求的父类型不同地配置依赖关系【代码】

我们正在使用StructureMap,并试图弄清楚如何实现以下目标. 我们有2个接口: ICat,IDog 对于ICat,我们需要使用Cat的实例. Cat构造函数采用ISound的实例. 对于IDog,我们需要使用Dog的实例. Dog构造函数也接受了一个ISound实例. ISound有一个实现AnimalSound. 到现在为止还挺好.这部分我们可以很容易地在结构图中做到. 现在问题出现在AnimalSound上. AnimalSound有一个构造函数,可以使用LoudSound或SoftSound进行初始化. (LoudSound和...

c# – 从其通用父类继承的嵌套类【代码】

这有可能以某种方式,这个场景,A.N从这个代码示例继承A的代码? 这样设置的原因是我需要从Base< TType>继承的多个类.和嵌套:基础< TType>服务器只有基数的地方,客户端有扩展的嵌套.通过这种方式,可以很容易地使用代码,在这些代码之间它们之间会有一些共享代码.彼此. 问题是我必须在内部编写相同的代码A and A.N B and B.N C and C.N 等等 我已经通过使用接口替换嵌套抽象类来暂时解决了这个问题A.N:A,INested,但现在我必须在所...

c# – 更改子类中的静态变量,而不在父类中更改它【代码】

我希望a.ID()返回0而b.ID()返回1,这是我的代码:public class A { public static int id; public int ID() {return id;} }public class B : A { }public class Main {void Program() { //This executes when I execute the programA.id = 0;B.id = 1;} }但它不起作用,这也行不通:public class A {public static int id;public int ID() {return id;} }public class B : A {public new static int id; //id is actually 1 but ID()...

c# – 区分父类对象中的子类?【代码】

我正在开发2个类似于下面的层次结构 – 层次结构1:public class Content { }public class Text : Content { }public class Image : Content { }层次结构2:public class ContentProperties { }public class TextProperties : ContentProperties { }public class ImageProperties : ContentProperties { }现在我想实现2个行为: 首先,我想在Content中使用ContentProperties的属性,以便我可以为所有Child类的Content概括它.然后,我...

c# – 如果父类和子类具有相同名称的静态方法,为什么它被认为是方法隐藏?【代码】

如果声明一个继承层次结构,其中父类和子类都具有相同名称和参数*的静态方法,则Visual Studio将引发警告CS0108: 例:public class BaseClass {public static void DoSomething(){} }public class SubClass : BaseClass {public static void DoSomething(){} }:警告CS0108:’SubClass.DoSomething()’隐藏继承的成员’BaseClass.DoSomething()’.如果要隐藏,请使用new关键字. 为什么这个方法被隐藏了?这两种方法都不涉及继承层次...