Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过控件对象读取自定义控件属性的值_C#_Custom Controls - Fatal编程技术网

C# 通过控件对象读取自定义控件属性的值

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

我有3个自定义控件,它们都有一个名为“MyCustomProperty”的属性,假设我需要使用

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很可能不是从
控件派生而来,而是希望从不同类型的控件派生(例如
文本框
标签
,等等).这个答案很有用,只要用你想用的控件替换
控件即可。“接口”救了我的命:)谢谢