C# 通过控件对象读取自定义控件属性的值
我有3个自定义控件,它们都有一个名为“MyCustomProperty”的属性,假设我需要使用C# 通过控件对象读取自定义控件属性的值,c#,custom-controls,C#,Custom Controls,我有3个自定义控件,它们都有一个名为“MyCustomProperty”的属性,假设我需要使用 foreach (control c in this.controls) 如何通过对象c访问MyCustomProperty 谢谢测试控件的类型是否正确,然后通过给它命名来强制转换它(myC此处): 以下是等效的,但可能更容易理解: if (c is MyCustomControl) { var myC = (MyCustomControl)c; var something = myC.My
foreach (control c in this.controls)
如何通过对象c访问MyCustomProperty
谢谢测试控件的类型是否正确,然后通过给它命名来强制转换它(
myC
此处):
以下是等效的,但可能更容易理解:
if (c is MyCustomControl)
{
var myC = (MyCustomControl)c;
var something = myC.MyCustomProperty;
}
测试控件的类型是否正确,然后通过给它命名来强制转换它(
myC
here):
以下是等效的,但可能更容易理解:
if (c is MyCustomControl)
{
var myC = (MyCustomControl)c;
var something = myC.MyCustomProperty;
}
您可以使用反射来识别属性中的控件,然后使用
动态
来访问属性:
using System.Reflection;
if (c.GetType().GetProperty("MyCustomProperty") != null)
{
string something = ((dynamic)c).MyCustomProperty; //Assuming your property is a string
}
您可以使用反射来识别属性中的控件,然后使用
动态
来访问属性:
using System.Reflection;
if (c.GetType().GetProperty("MyCustomProperty") != null)
{
string something = ((dynamic)c).MyCustomProperty; //Assuming your property is a string
}
第一步。为控件提供公开属性的公共接口
interface IPropertyHolder
{
string MyCustomProperty { get; }
}
class MyCustomControl1 : TextBox, IPropertyHolder
{
public string MyCustomProperty { get; set; }
}
class MyCustomControl2 : Form, IPropertyHolder
{
public string MyCustomProperty { get; set; }
}
class MyCustomControl3 : Control, IPropertyHolder
{
public string MyCustomProperty { get; set; }
}
第二步。要么投下它:
foreach (var c in this.controls)
{
var custom = c as IPropertyHolder;
if (custom != null)
{
var temp = c.MyCustomProperty;
}
}
或仅包括具有以下界面的控件:
foreach (var c in this.controls.OfType<IPropertyHolder>())
{
var temp = c.MyCustomProperty;
}
foreach(this.controls.OfType()中的var c)
{
var temp=c.MyCustomProperty;
}
第1步。为控件提供公开属性的公共接口
interface IPropertyHolder
{
string MyCustomProperty { get; }
}
class MyCustomControl1 : TextBox, IPropertyHolder
{
public string MyCustomProperty { get; set; }
}
class MyCustomControl2 : Form, IPropertyHolder
{
public string MyCustomProperty { get; set; }
}
class MyCustomControl3 : Control, IPropertyHolder
{
public string MyCustomProperty { get; set; }
}
第二步。要么投下它:
foreach (var c in this.controls)
{
var custom = c as IPropertyHolder;
if (custom != null)
{
var temp = c.MyCustomProperty;
}
}
或仅包括具有以下界面的控件:
foreach (var c in this.controls.OfType<IPropertyHolder>())
{
var temp = c.MyCustomProperty;
}
foreach(this.controls.OfType()中的var c)
{
var temp=c.MyCustomProperty;
}
我有3个不同的自定义控件(不同类型,但它们共享相同的属性,是否有类似于c.properties(“MyCustomProperty”).value的内容?@Hayanamamoun请看我的新答案。我有3个不同的自定义控件(不同类型,但它们共享相同的属性,是否有类似于c.properties(“MyCustomProperty”)的内容).value?@Hayanamamoun查看我的新答案。谢谢,有没有其他方法获取属性的值?演示的方法要求我添加Microsoft.CSharp并切换到更高的.net framework,这是我由于某些原因无法做到的。Hanks,有没有其他方法获取属性的值?演示的方法要求我添加Microsoft.CSharp并切换到更高的.net框架,出于某些原因,我无法这样做。OP可能希望从不同类型的控件(例如文本框
,标签
)派生,而不是从控件派生。这个答案可以解决这个问题,只要用你想要使用的控件替换控件即可。“接口”救了我的命:)谢谢。OP很可能不是从控件派生而来,而是希望从不同类型的控件派生(例如文本框
,标签
,等等).这个答案很有用,只要用你想用的控件替换控件即可。“接口”救了我的命:)谢谢