English 中文(简体)
a. 在系统添加财产吗?
原标题:Dynamically add a Property to a System.object?

我拿着我在不同类别中定义的不同物体清单,我想在所有这些物体上添加“Name”的描述性财产。 这是可能的吗?

I don t have that much code to provide as my classes are very simple/classic ones.

Thanks in advance for any help !

(edit :我不想继承增加这一财产的抽象类别!) 事实上,我不想在界定我的反对的所有类别上加以修改。 这在标题中称为“有意思的”。

我想的是:

myObject.AddProperty(string, "Name");

or

myObject.AddAttribute(string, "Name");

(我不知道究竟是怎么说)

之后,我可以:

myObject.Name = "blaaa";
最佳回答

1. 创建贵国所有其他阶层都能继承的抽象课程:

public abstract class MyBaseClass
{
    public string MyCommonString { get; set; }
}

public class Foo : MyBaseClass
{
    public MyBaseClass() { }
}

//Create instance of foo
Foo myFoo = new Foo();

//MyCommonString is accessible since you inherited from base
string commonString = myFoo.MyCommonString;

<EDIT (根据新的要求)

由于你不想触及LLL的原始班级,我采取这一[类似]做法:

public abstract class MyBaseClass
{
    public string MyCommonString { get; set; }
}

//This class definition lives in the DLL and remains untouched
public class Foo
{
    public Foo() { }
}

//This partial class definition lives in [insert new project name here]
public partial class Foo : MyBaseClass
{
    public Foo () { }
}

Notice that Foo is now a partial class. You re not touching the existing class definition in the DLL, you re extending it.

<EDIT (根据新的要求)

鉴于您的要求(没有编辑原有的班级),你再次要求的话是不可能的。

问题回答

What you can do is to hard code a Hashtable named CustomProperties Now you can fill this Hashtable with custom properties

与此类似:

        MyClass myClass = new MyClass();
        myClass.SetProperty("abc", 123);
        myClass.SetProperty("bcd", "bla");
        myClass.SetProperty("cde", DateTime.Now);

        MessageBox.Show(myClass.GetProperty("abc").ToString());

class MyClass
{
    private Hashtable MyProperties { get; set; }

    public MyClass()
    {
        MyProperties = new Hashtable();
    }

    public object GetProperty(string name)
    {
        return MyProperties.Contains(name) ? MyProperties[name] : null;
    }

    public void SetProperty(string name, object value)
    {
        if (MyProperties.Contains(name))
            MyProperties[name] = value;
        else
            MyProperties.Add(name, value);
    }
}

请使用新的C# 4.0dynamic 关键词:

dynamic obj = new System.Dynamic.ExpandoObject();
obj.Value = 10;
var action = new Action<string>((l) => Console.WriteLine(l));
obj.WriteNow = action;
obj.WriteNow(obj.Value.ToString());

You can not do this with object, but the ExpandoObject will do just fine.

但是,......在不远的将来,过度使用动态打字,你在夜里发现ll。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签