我对仅将第一个界面投射到 Javascript 的限制有疑问。我想知道是否有任何解决方法/解决方法。
我有大约 20 个我想用 Javascript 访问的课程。这 20 个类继承自 BaseModel,其中包含所有 20 个类都使用的重要代码。下面是一个简单的例子:
public class Category : BaseModel
{
public string Title {get;set;}
public string Description {get;set;}
}
public class Item : BaseModel
{
public string Title {get;set;}
public string Price {get;set;}
}
public class BaseModel : INotifyPropertyChanged, IParentableViewModel
{
//Some Important Methods that are shared between around 20 Models
}
如果我想用 Javascript 访问它,我必须为所有 20 个类创建 BaseModel。这是因为 Javascript 只能看到第一个接口的属性/方法。那么我的代码就变成了这样:
public class CategoryBaseModel : ICategoryModel, INotifyPropertyChanged, IParentableViewModel
{
//Important Methods gets duplicated between CategoryBaseModel and ItemBaseModel making it harder to maintain
}
public class ItemBaseModel : IItemModel, INotifyPropertyChanged, IParentableViewModel
{
//Important Methods gets duplicated between CategoryBaseModel and ItemBaseModel making it harder to maintain
}
这变得难以维护,特别是因为我有大约 20 种方法。
我正在寻找是否可以覆盖 Javascript 的设置器,但不知道该怎么做。这个想法是这样的:
//I want to override something like this.
//whenever I call category.someProperty = someValue; in Javascript, it goes through this.
//When it goes through this, I can then call the SetProperty method for unknown properties in the interface
public bool JavaScriptPropertySet(object, propertyName, Value)
{
if(object.hasProperty(propertyName))
{
//Set PropertyName
}
else
{
object.setProperty(propertyName, Value);
}
}
然后我可以像这样定义我的类更简单:
public interface IBaseViewModel
{
string Title {get;set;}
void SetProperty(propertyName, Value);
}
public class BaseModel : IBaseViewModel, INotifyPropertyChanged, IParentableViewModel
{
//important methods
public void SetProperty(propertyName, Value)
{
SetPropertyInternal(propertyName, Value);
}
public virtual void SetPropertyInternal(propertyName, Value)
{
}
}
public class Category : BaseModel
{
public override void SetPropertyInternal(propertyName, Value)
{
if(propertyName == 'description')
this.description = Value;
}
}
有没有这样的方法?
谢谢