我遇到的问题是只将第一个界面投射到Javascript。我想知道是否有任何解决方法/解决方法。
我想在Javascript中访问大约20个类。这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的setter但不知道该怎么做。这个想法是这样的:
//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;
}
}
有这样的方法吗?
谢谢