有没有办法在另一个方法中停止运行循环或在C#中动态插入break语句?
谢谢
编辑:我希望能够动态拦截方法并插入一个中断以在另一个函数中触发事件时停止循环.我有几个类的实例,我想在每个实例中停止循环,只要需要和管理所有的例子.考虑将多个实例放在通用列表中
示例:
List<myclass> objlist=new List<myclass>();
foreach(myclass obj in objlist)
{
obj.loopingfunction().BreakLoop //or something like this (assuming that the loopingfunction is already called)
}
Run Code Online (Sandbox Code Playgroud)
我需要这个,因为我想在用户存储大量数据后打破循环.当用户导入数据时,我会触发一个事件.但我不能继续从多个实例检查数据库,因为它搞砸了sqlserver.
这是在ASP.Net应用程序中.
我有一个.net测试类.在Initialize方法中,我创建了一个windsor容器并进行了一些注册.在实际的测试方法中,我在控制器类上调用一个方法,但是拦截器不起作用,并且直接调用该方法.这有什么潜在的原因?
这是所有相关代码:
test.cs中:
private SomeController _someController;
[TestInitialize]
public void Initialize()
{
Container.Register(Component.For<SomeInterceptor>());
Container.Register(
Component.For<SomeController>()
.ImplementedBy<SomeController>()
.Interceptors(InterceptorReference.ForType<SomeInterceptor>())
.SelectedWith(new DefaultInterceptorSelector())
.Anywhere);
_someController = Container.Resolve<SomeController>();
}
[TestMethod]
public void Should_Do_Something()
{
_someController.SomeMethod(new SomeParameter());
}
Run Code Online (Sandbox Code Playgroud)
SomeController.cs:
[HttpPost]
public JsonResult SomeMethod(SomeParameter parameter)
{
throw new Exception("Hello");
}
Run Code Online (Sandbox Code Playgroud)
SomeInterceptor.cs:
public class SomeInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// This does not gets called in test but gets called in production
try
{
invocation.Proceed();
}
catch
{
invocation.ReturnValue = new SomeClass();
}
}
} …Run Code Online (Sandbox Code Playgroud) 我知道这已经讨论了很多次.我只是无法理解这项工作或我的错误在哪里.
我想给你一个简化的例子是向你展示我正在尝试做什么以及我正在做什么假设的最佳方式......
我有一个名称的Product类.该名称是一个惰性的String属性.
我的DAO:
public abstract class HibernateProductDAO extends HibernateDaoSupport implements ProductDAO
{
public List getAll()
{
return this.getHibernateTemplate().find("from " + this.getDomainClass().getSimpleName());
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务界面:
public interface ProductService {
//This methods are Transactional, but same exception error is thrown if there weren't
@Transactional
public Product getProduct();
@Transactional
public String getName(Product tp);
}
Run Code Online (Sandbox Code Playgroud)
我的服务实施:
public class ProductServiceImpl implements ProductService {
private ProductDAO productDAO;
public Product getProduct() {
List ps = this.productDAO.getAll();
return (Product) ps.get(0);
}
public String getName(Product p){
return p.getName(); …Run Code Online (Sandbox Code Playgroud) 假设我们有两个bean,在Spring中定义
<bean class="foo.A"/>
<bean class="foo.B"/>
Run Code Online (Sandbox Code Playgroud)
public class A {
@Autowired
private B b;
}
public class B {
public void foo() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是截取所有调用B.foo().看一下文档,我写了拦截器C并改变了bean的定义B如下:
public class C implements org.springframework.aop.MethodBeforeAdvice {
public void before(final Method method, final Object[] args, final Object target) {
// interception logic goes here
}
}
Run Code Online (Sandbox Code Playgroud)
<bean class="foo.C"/>
<bean class="org.springframework.aop.framework.ProxyFactoryBean" scope="prototype">
<property name="proxyTargetClass" value="true"/>
<property name="singleton" value="false"/>
<property name="target">
<bean class="foo.B" scope="prototype"/>
</property>
<property name="interceptorNames">
<list>
<value>foo.C</value>
</list>
</property> …Run Code Online (Sandbox Code Playgroud) 我们有两个拦截器.输入拦截器Phase.RECEIVE和输出拦截器Phase.SETUP_ENDING
public class BeforeInterceptor extends AbstractPhaseInterceptor<Message>
{
public BeforeInterceptor()
{
super(Phase.RECEIVE);
}
Run Code Online (Sandbox Code Playgroud)
和
public class AfterInterceptor extends AbstractPhaseInterceptor<Message>
{
public AfterInterceptor()
{
super(Phase.SETUP_ENDING);
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道:这两个阶段之间有多少时间?
我必须调用System.currentTimeMillis();BeforeInterceptor,将此值转换为AfterInterceptor,并
System.currentTimeMillis() - valueFromBeforeInterceptor在拦截器后调用.
但是如何从另一个拦截器传输数据呢?
Akka和Scala新手在这里,请随时编辑这个问题,以便清楚地表达我在Scala和Akka领域的意图.
在我展示代码片段之前,这是我想要解决的问题:我本质上想要为我的团队开发一个通用模块,以便在他们使用Akka actor开发应用程序时使用.我想让它们混合一个特性,它将在运行时扩展它们的接收功能,主要用于记录目的.我遇到了编译错误,我很快就会解释.
但首先,举一个简单的主要内容:
object Test extends App {
val system = ActorSystem("system")
val myActor = system.actorOf(Props(new MyActor), "myActor")
myActor ! "Hello world!"
}
Run Code Online (Sandbox Code Playgroud)
以下是团队成员可能在其应用程序中实现的actor的示例实现:
class MyActor extends Actor with ActorLogger {
override def receive: Receive = {
case msg => {
log.info("testing ...")
}
case _ => throw new RuntimeException("Runtime Ex")
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个如何为他们提供混合的共同特征的例子:
trait ActorLogger extends Actor {
val log: DiagnosticLoggingAdapter = Logging(this)
abstract override def receive: Receive = {
case msg: Any => {
if (msg.isInstanceOf[String]) { …Run Code Online (Sandbox Code Playgroud) 专家/大师/友
我们正在使用Spring 3.2,JPA 2,Hibernate 4.2组合并面对这个奇怪的空指针问题,同时尝试将任何spring注释bean注入到EmtyInterceptor中,如下所示.我们尝试过注释这个bean以及一个spring bean但没有运气.
任何帮助解决这个难题的人都非常感谢.
import javax.inject.Inject;
import javax.inject.Named;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.transaction.annotation.Transactional;
...
@Named
@Transactional
public class AuditEmptyInterceptor extends EmptyInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
// Didnt inject - Null
@PersistenceContext
private EntityManager entityManager;
// Didnt inject - Null
//@PersistenceUnit
//private EntityManagerFactory entityManagerFactory;
// Didnt inject - Null
//@Inject
//private AuditHelper auditHelper;
@Override
public boolean onSave(Object entity, Serializable id, Object[] currentState,
String[] propertyNames, Type[] types) {
System.out.println("**********inside OnSave() in …Run Code Online (Sandbox Code Playgroud) 我的拦截器(验证)没有在动作之前或之后被调用.任何想法如何让它工作?
注意:每次调用默认拦截器时.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default,json-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
<result-type name="json" class="org.apache.struts2.json.JSONResult" />
</result-types>
<interceptors>
<interceptor name="validation" class="ValidatorBaseAction"/>
<interceptor-stack name="default">
<interceptor-ref name="logger"/>
</interceptor-stack>
<interceptor-stack name="validationStack">
<interceptor-ref name="validation"/>
<interceptor-ref name="default"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="default" />
<action
name="viewRequest"
class="ViewAction"
method="execute">
<interceptor-ref name="validationStack" />
<result name="input" type="redirectAction">explore</result>
<result name="success" type="redirect">/showRequest.do?${explorerParameters}</result>
</action>
</package>
</struts>
Run Code Online (Sandbox Code Playgroud) java struts2 interceptor interceptorstack struts2-interceptors
我正在使用Retrofit 2开发一个应用程序以请求API。该API位于ASP.NET中,并且使用GZip压缩并编码为Base64,如以下代码所示:
private static string Compress(string conteudo)
{
Encoding encoding = Encoding.UTF8;
byte[] raw = encoding.GetBytes(conteudo);
using (var memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true))
{
gzip.Write(raw, 0, raw.Length);
}
return Convert.ToBase64String(memory.ToArray());
}
}
private static string Decompress(string conteudo)
{
Encoding encoding = Encoding.UTF8;
var gzip = Convert.FromBase64String(conteudo);
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
int size = gzip.Length;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int …Run Code Online (Sandbox Code Playgroud) 我想知道在验证错误后如何重新发送带有httpinterceptor的请求?
我检查是否有错误(刷新我的JWT令牌),刷新后我想再次提交失败请求。
httpinterceptor.js:
import { Observable } from 'rxjs';
import { Injectable, Inject, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { UserService } from "./user/services/user.service";
import { Router } from "@angular/router";
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
public userService;
constructor(private router: Router,
private injector: Injector) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const clonedRequest = req.clone();
this.userService = this.injector.get(UserService);
return next.handle(req)
.do(event => {
if (event instanceof HttpResponse) {
//normal …Run Code Online (Sandbox Code Playgroud)