我有下面这段代码,作为一个例子,dec_proxy尝试将增量运算符的效果反转到在复杂函数调用foo中执行的类型 - 顺便说一下我无法更改接口.
#include <iostream>
template<typename T>
class dec_proxy
{
public:
dec_proxy(T& t)
:t_(t)
{}
dec_proxy<T>& operator++()
{
--t_;
return *this;
}
private:
T& t_;
};
template<typename T, typename S, typename R>
void foo(T& t, S& s, R& r)
{
++t;
++s;
++r;
}
int main()
{
int i = 0;
double j = 0;
short k = 0;
dec_proxy<int> dp1(i);
dec_proxy<double> dp2(j);
dec_proxy<short> dp3(k);
foo(dp1,dp2,dp3);
//foo(dec_proxy<int>(i), <---- Gives an error
// dec_proxy<double>(j), <---- Gives an error
// dec_proxy<short>(k)); <---- …Run Code Online (Sandbox Code Playgroud) 我有一个带有这样签名的web方法:
public string[] ToUpper(string[] values)
Run Code Online (Sandbox Code Playgroud)
我在Visual Studio 2010中使用"添加服务引用"来生成对我的服务的引用.不幸的是,这个过程创建了一个名为'ArrayOfString'的代理类,并使用这种类型而不是预期的'string []'类型.生成的异步服务调用签名最终看起来像这样:
public void ToUpperAsync(Demo.ServiceReference.ArrayOfString values) { }
public void ToUpperAsync(Demo.ServiceReference.ArrayOfString values, object userState) { }
Run Code Online (Sandbox Code Playgroud)
我已经尝试了配置服务参考表单中"收集"下拉列表的所有选项,但它似乎并没有什么区别.
这在以前工作,但由于某种原因它可能突然停止工作,可能是从服务中删除另一个Web方法.
如何让生成的服务引用类使用string []类型而不是生成的ArrayOfString类型?任何有关这方面的帮助将不胜感激.
编辑: 正如@Oleg建议的那样,我正在使用ASMX Web服务.
假设我有一个界面
class I{
public:
virtual void f(int id)=0;
virtual void g(int id, float x)=0;
}
Run Code Online (Sandbox Code Playgroud)
我需要一个代理类,为指针映射做一些id
class Proxy : I
{
I * i[5];
public:
void f(int id)
{
i[id]->f(id);
}
void g(int id, float x)
{
i[id]->g(id, x);
}
}
Run Code Online (Sandbox Code Playgroud)
所以当我写作
Proxy *p;
p->f(1);
Run Code Online (Sandbox Code Playgroud)
在id = 1的对象上调用f
有几个这样的情况和接口相当大.所以我不想编写代理类中的所有函数.有没有办法自动完成?也许使用宏,模板,重载" - >"等.
pry非常适合调试BasicObject的子类!
https://github.com/pry/pry说pry有:"Exotic对象支持(BasicObject实例......"
但是怎么做呢?可以预料,BasicObject不理解绑定.
NameError:
undefined local variable or method `binding' for #<C30Course:0xbefbc0c>
Run Code Online (Sandbox Code Playgroud)
调用method_missing时,何处发送绑定?
我正在使用装饰器模式List<WebElement>.这个装饰的部分需要使用代理.
当我get(index)使用超出范围的索引调用时,它会抛出一个IndexOutOfBounds异常,然后由代理捕获,并用一个包装UndeclaredThrowableException.
我的理解是它应该只在它被检查的异常时才这样做. IndexOutOfBounds是一个未经检查的异常,为什么它被包裹?
即使我添加throws IndexOutOfBounds到我的invoke函数,它仍然会被包装.
这是我的代码:
@SuppressWarnings("unchecked")
public WebElementList findWebElementList(final By by){
return new WebElementList(
(List<WebElement>) Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class<?>[] { List.class }, new InvocationHandler() {
// Lazy initialized instance of WebElement
private List<WebElement> webElements;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (webElements == null) {
webElements = findElements(by);
}
return method.invoke(webElements, args);
}
}), driver); …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio 2013从此WSDL文件生成WCF服务代理.但是,只要我尝试调用该setSalesItemsV3方法,WCF就会InvalidOperationException从中深入抛出System.Xml.dll.
此示例项目演示了此问题:https://github.com/jennings/WsdlDuplicateNameProblem
这是内在的例外:
消息:名称空间中的顶级XML元素"start"引用了不同类型WsdlDuplicateName.SalesItemService.hsSimpleDate和System.DateTime.使用XML属性为元素指定另一个XML名称或命名空间.
我不是读WSDL的专家,但是我已经看了它,并且引用名称"start"的唯一部分是以下几个<wsdl:part>元素name="start":
<wsdl:message name="setSalesItems">
<wsdl:part name="start" type="xsd:dateTime"></wsdl:part>
</wsdl:message>
<wsdl:message name="setSalesItemsV3">
<wsdl:part name="start" type="tns:hsSimpleDate"></wsdl:part>
</wsdl:message>
Run Code Online (Sandbox Code Playgroud)
但是,这些部分是完全不同的消息,所以我不明白为什么会有任何混淆.我通过几个在线WSDL验证器运行WSDL文件,它们似乎没问题.
下面是重现问题所需的项目中唯一的代码(除了生成的代理).
class Program
{
static void Main(string[] args)
{
SalesServiceClient client = new SalesServiceClient();
var date = ToSimpleDate(new DateTime());
// throws InvalidOperationException
// Message == "There was an error reflecting 'start'."
client.setSalesItemsV3(1, 1, null, date, date);
}
static hsSimpleDate ToSimpleDate(DateTime time)
{
return new …Run Code Online (Sandbox Code Playgroud) 我在Java中遇到问题,我在其中设置了一个带有JMX接口的动态代理,将其传递给另一个组件,然后调用代理对象.当我这样做时,应用程序为每个调用泄漏两个线程,线程似乎永远不会超时并继续构建,直到应用程序内存不足.
线程成对出现,请参见底部的stacktrace.
我曾尝试使用一些略显模糊的系统属性来关闭JMX中的超时,但它并没有什么区别.关键动作似乎是动态代理调用.通过代理调用的对象实现Serializable,因此不应该是一个问题.
当我手动创建一个包含MBean路径和对象接口的字符串的Bean并从中调用该方法时,问题就消失了.
在动态代理方面,我主要是在寻找经典的问题,因为我没有太多的经验.
这就是创建proxyinstance的方式
public <T> T create(final Class<T> type,
final Object... nameParameters) throws JmxConnectionException {
return type.cast(Proxy.newProxyInstance(
type.getClassLoader(),
new Class< ? >[] {type},
new MyInvocationHandler(this,
fill(nameOf(type), nameParameters))));
}
Run Code Online (Sandbox Code Playgroud)
和MyInvocationHandler的实现:
final class MyInvocationHandler implements InvocationHandler, Serializable {
private static final long serialVersionUID = 0L; //actually a proper random long
private final transient ProxyFactory proxyFactory;
private String mBeanName;
private RemoteObject remoteObject;
MyInvocationHandler(final ProxyFactory proxyFactory,
final String mBeanName) {
this.proxyFactory = proxyFactory;
this.mBeanName = mBeanName;
}
private void writeObject(final ObjectOutputStream …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法让生成的Web引用代理类(而不是WCF)实现一个通用接口,以便在客户端应用程序中轻松切换Web服务访问和"直接"访问我们的业务层,如:
public IBusiness GetBusinessObject()
{
if (_mode = "remote")
return new BusinessWebService.Business(); // access through web service proxy class
else
return new Business(); // direct access
}
Run Code Online (Sandbox Code Playgroud)
但是,CustomSerializableType生成的代理类中未引用自定义类型(例如,下面的示例中的自定义类型).而是生成新的相同类型,这使得代理类无法实现接口.
是否有某种方法可以使生成的代理类引用这些类型,或者我是否认为这一切都错了?我应该考虑将Web服务转换为WCF服务吗?
细节
我们的解决方案包括以下四个项目:
CustomSerializableType)我们的客户希望Windows应用程序能够以两种不同的模式运行:
为此,我们创建了一个IBusiness接口,它位于公共库中,包含所有业务方法.
接口
public interface IBusiness
{
CustomSerializableType DoSomeWork();
}
Run Code Online (Sandbox Code Playgroud)
业务层
public class Business : IBusiness
{
public CustomSerializableType DoSomeWork()
{
// access data store
}
}
Run Code Online (Sandbox Code Playgroud)
网络服务
public class WebServiceBusiness : IBusiness
{
private Business _business = new …Run Code Online (Sandbox Code Playgroud) 我有以下代码,用于为InvocationHandler实现支持的接口类型创建代理实例,但是当我使用具体的类类型时,它不起作用,这是众所周知的,并在Proxy.newProxyInstance中记录:
// NOTE: does not work because SomeConcreteClass is not an interface type
final ClassLoader myClassLoader = SomeConcreteClass.getClassLoader();
SomeConcreteClass myProxy = (SomeConcreteClass) Proxy.newProxyInstance(myClassLoader, new Class[] {
SomeConcreteClass.class }, new InvocationHandler() { /* TODO */ });
Run Code Online (Sandbox Code Playgroud)
但是,如果我没记错的话,我已经在一些模拟框架中看到了这个用例,它可以模拟一个具体的类类型,例如EasyMock.在检查他们的源代码之前,任何人都可以指出需要做什么来代理具体的类类型而不仅仅是接口吗?
我正在开发一个我没有启动的 Django 项目,并且面临继承问题。
我有一个大模型(在示例中简化)称为MyModel它应该代表不同类型的项目。
的所有实例对象MyModel都应具有相同的字段,但方法行为根据项目类型而有很大差异。
到目前为止,它是使用一个MyModel名为 的单个字段来设计的item_type。
然后 MyModel 中定义的方法检查该字段并使用多个 if 执行不同的逻辑:
def example_method(self):
if self.item_type == TYPE_A:
do_this()
if self.item_type == TYPE_B1:
do_that()
Run Code Online (Sandbox Code Playgroud)
更重要的是,某些子类型有许多共同点,因此可以说子类型B和C代表第一级继承。然后这些类型有子类型,例如B1, B2, C1, C2(在下面的示例代码中更好地解释)。
我想说这不是执行多态性的最佳方法。
现在我想更改这些模型以使用真正的继承。
由于所有子模型都有相同的字段,我认为多表继承是没有必要的。我正在考虑使用代理模型,因为只有它们的行为应该根据它们的类型而改变。
这是我提出的伪解决方案:
ITEM_TYPE_CHOICES = (
(TYPE_A, _('Type A')),
(TYPE_B1, _('Type B1')),
(TYPE_B2, _('Type B2')),
(TYPE_C1, _('Type C1')),
(TYPE_C2, _('Type C2')))
class MyModel(models.Model):
item_type = models.CharField(max_length=12, choices=ITEM_TYPE_CHOICES)
def common_thing(self):
pass …Run Code Online (Sandbox Code Playgroud) python django proxy-classes django-models django-inheritance