C# 我可以定义一个对类类型和类实例都可用的属性吗?
我有一个基类接口,从基类继承的每个类都应该有一个标识字段,告诉应用程序它是什么类型的对象 我想以两种不同的方式使用此属性: 不创建对象的实例C# 我可以定义一个对类类型和类实例都可用的属性吗?,c#,inheritance,C#,Inheritance,我有一个基类接口,从基类继承的每个类都应该有一个标识字段,告诉应用程序它是什么类型的对象 我想以两种不同的方式使用此属性: 不创建对象的实例 if (someValue == TestA.Id) return new TestA(); elseif (someValue == TestB.Id) return new TestB(); 和作为接口的属性 void DoSomething(ITest testObject) { SomeValue = testObjec
if (someValue == TestA.Id)
return new TestA();
elseif (someValue == TestB.Id)
return new TestB();
和作为接口的属性
void DoSomething(ITest testObject)
{
SomeValue = testObject.Id;
}
是否有一种简单的方法来定义接口中的Id
字段,但在不创建类实例的情况下仍然可以使用它
现在我正在使用以下代码。我可以在返回常量字符串的接口中添加一个只读的Id
属性,但是我希望有一种我不知道的更简单的方法
public interface ITest
{
}
public class TestA : ITest
{
public const string Id = "A";
}
Rachel,我也遇到过类似的问题,我总是(不幸地)求助于让工厂代码依赖于反射来获得每个具体类型的“TypeID”公共静态属性。。。因此,创建了合同接口的另一个方面,但在C#接口代码中没有它。我遇到了一个类似的问题,Rachel,我总是(不幸地)求助于让工厂代码依赖于反射来获得每个具体类型的“TypeID”公共静态属性。。。因此,在合同接口中增加了一个方面,但在C#接口代码中没有它。简言之,没有 为了能够做到这一点,您需要能够将其指定为接口上的实例属性(并在实例中实现),以及类型上的静态属性 编译器不允许您这样做。简而言之-不 为了能够做到这一点,您需要能够将其指定为接口上的实例属性(并在实例中实现),以及类型上的静态属性
编译器不允许您这样做。您可以将它放在接口中,也可以将其作为静态属性。比如:
interface IInterface { Id { get; } }
class Class : IInterface
{
public static Id { get { return 1; } }
public Id { get { return Class.Id; } }
}
您可以将它放在接口中,也可以将其作为静态属性。比如:
interface IInterface { Id { get; } }
class Class : IInterface
{
public static Id { get { return 1; } }
public Id { get { return Class.Id; } }
}
你可以这样做
public interface ITest
{
SomeValue Id{ get;}
}
public class TestA : ITest
{
public SomeValue Id
{
get {return TestA.StaicId; }
}
public static SomeValue StaticId
{
get {return "This is TestA";}
}
}
if (someValue == TestA.StaticId)
return new TestA();
你可以这样做
public interface ITest
{
SomeValue Id{ get;}
}
public class TestA : ITest
{
public SomeValue Id
{
get {return TestA.StaicId; }
}
public static SomeValue StaticId
{
get {return "This is TestA";}
}
}
if (someValue == TestA.StaticId)
return new TestA();
使用属性怎么样?下面是一个可以做的小例子:
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class IdAttribute : Attribute
{
public IdAttribute(string id)
{
this.Id = id;
}
public string Id { get; set; }
}
public interface IMyInterface
{
}
public abstract class BaseClass : IMyInterface
{
public static string GetId<T>() where T : IMyInterface
{
return ((IdAttribute)typeof(T).GetCustomAttributes(typeof(IdAttribute), true)[0]).Id;
}
}
[Id("A")]
public class ImplA : BaseClass
{
}
[Id("B")]
public class ImplB : BaseClass
{
}
internal class Program
{
private static void Main(string[] args)
{
var val1 = BaseClass.GetId<ImplA>();
var val2 = BaseClass.GetId<ImplB>();
Console.ReadKey();
}
}
[AttributeUsage(AttributeTargets.Class,Inherited=false,AllowMultiple=false)]
公共类IdAttribute:属性
{
公共IdAttribute(字符串id)
{
这个.Id=Id;
}
公共字符串Id{get;set;}
}
公共接口IMyInterface
{
}
公共抽象类基类:IMyInterface
{
公共静态字符串GetId(),其中T:IMyInterface
{
返回((IdAttribute)typeof(T).GetCustomAttributes(typeof(IdAttribute),true)[0]).Id;
}
}
[Id(“A”)]
公共类ImplA:基类
{
}
[Id(“B”)]
公共类ImplB:基类
{
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
var val1=BaseClass.GetId();
var val2=BaseClass.GetId();
Console.ReadKey();
}
}
如何使用属性?下面是一个可以做的小例子:
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class IdAttribute : Attribute
{
public IdAttribute(string id)
{
this.Id = id;
}
public string Id { get; set; }
}
public interface IMyInterface
{
}
public abstract class BaseClass : IMyInterface
{
public static string GetId<T>() where T : IMyInterface
{
return ((IdAttribute)typeof(T).GetCustomAttributes(typeof(IdAttribute), true)[0]).Id;
}
}
[Id("A")]
public class ImplA : BaseClass
{
}
[Id("B")]
public class ImplB : BaseClass
{
}
internal class Program
{
private static void Main(string[] args)
{
var val1 = BaseClass.GetId<ImplA>();
var val2 = BaseClass.GetId<ImplB>();
Console.ReadKey();
}
}
[AttributeUsage(AttributeTargets.Class,Inherited=false,AllowMultiple=false)]
公共类IdAttribute:属性
{
公共IdAttribute(字符串id)
{
这个.Id=Id;
}
公共字符串Id{get;set;}
}
公共接口IMyInterface
{
}
公共抽象类基类:IMyInterface
{
公共静态字符串GetId(),其中T:IMyInterface
{
返回((IdAttribute)typeof(T).GetCustomAttributes(typeof(IdAttribute),true)[0]).Id;
}
}
[Id(“A”)]
公共类ImplA:基类
{
}
[Id(“B”)]
公共类ImplB:基类
{
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
var val1=BaseClass.GetId();
var val2=BaseClass.GetId();
Console.ReadKey();
}
}
这就是你想要做的:这就是你想要做的:好的,谢谢。我自己没能弄明白,但我想在完全放弃之前我会在这里问一下,因为C#有很多东西,我仍然不知道我能看出它有多大用处,但唉,没有。好吧,谢谢你。我自己没能弄明白,但我想在完全放弃之前我会在这里问一下,因为C#有很多东西,我仍然不知道我能看出它有多大用处,但遗憾的是,没有。是的,如果没有更简单的方法,我计划这么做。关于C#我还不知道,所以我希望有办法创建两个属性。是的,如果没有更简单的方法,这就是我计划做的。关于C#我还不知道,所以我希望有一种方法可以创建两个属性。我想到了这一点,但希望有一种更简单的方法。既然没有,我打算在我的电脑上公开第二份,就像凯尔建议的那样。我想到了这一点,但希望有一个更简单的方法。既然没有,我计划只在我的电脑上公开第二个副本,就像凯尔建议的那样。