小编Joa*_*oao的帖子

在运行时添加属性

我有一个程序员可以用来动态添加新属性的类.为此,它实现了ICustomTypeDescriptor能够覆盖GetProperties()方法.

public class DynamicProperty
{
    public object Value { get; set; }

    public Type Type { get; set; }

    public string Name { get; set; }

    public Collection<Attribute> Attributes { get; set; }
}

public class DynamicClass : ICustomTypeDescriptor
{
    // Collection to code add dynamic properties
    public KeyedCollection<string, DynamicProperty> Properties
    {
        get;
        private set;
    }

    // ICustomTypeDescriptor implementation
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
        // Properties founded within instance
        PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // Fill property …
Run Code Online (Sandbox Code Playgroud)

c# runtime properties

15
推荐指数
1
解决办法
2万
查看次数

Rails,销毁与删除?

为什么这样做:

@poll_votes = PollVote.where(:user_id => self.user_id, :poll_id => self.poll_id).all

@poll_votes.each do |p|
  p.destroy
end
Run Code Online (Sandbox Code Playgroud)

但这不是吗?

@poll_votes = PollVote.where(:user_id => self.user_id, :poll_id => self.poll_id).destroy
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3

5
推荐指数
2
解决办法
6146
查看次数

在运行时维护功能更改对象类型

长话短说

说我有以下代码:

// a class like this 
class FirstObject {
    public Object OneProperty {
        get;
        set;
    }

    // (other properties)

    public Object OneMethod() {
        // logic
    }
}

// and another class with properties and methods names 
// which are similar or exact the same if needed 
class SecondObject {
    public Object OneProperty {
        get;
        set;
    }

    // (other properties)

    public Object OneMethod(String canHaveParameters) {
        // logic
    }
}

// the consuming code would be something like this 
public static …
Run Code Online (Sandbox Code Playgroud)

c# reflection types runtime

4
推荐指数
1
解决办法
6506
查看次数