我有一个程序员可以用来动态添加新属性的类.为此,它实现了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) 为什么这样做:
@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) 说我有以下代码:
// 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)