This question already has an answer here:
I have two classes...
public enum MyType
{
type1,
type2
}
[Sample(MyType.type2)]
public class Person
{
}
[Sample(type = MyType.type1)]
public class People
{
}
and an attribute class ...
[AttributeUsage(AttributeTargets.All)]
public class SampleAttribute : Attribute
{
private MyType type { get; set; } = MyType.type1;
public SampleAttribute(MyType _type)
{
type = _type;
}
}
and following code into the Form_Load event...
var types = from t in Assembly.GetExecutingAssembly().GetTypes() select t;
foreach (Type type in types)
{
if (type.GetCustomAttribute<SampleAttribute>().type == MyType.type1)
{
listBox1.Items.Add(type.Name);
}
}
I wish to get a list of such classes for which I have set the custom attribute's property to type1
This code generates a run-time error saying..."object reference not set to an instance of an object"
Any suggestion? where and what I have missed to initialize? Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire