我有一个需要很长时间才能初始化的对象。因此我能够在应用程序启动时开始初始化。任何对类上方法的后续调用都需要有一个延迟机制来等待类完成初始化。
我有几个潜在的解决方案,但我对它们中的任何一个都不完全满意。第一个在 while 循环中使用 Task.Delay,第二个使用 SemaphoreSlim 但涉及一些不必要的阻塞。我觉得这一定是一个相当普遍的要求,有人可以就如何最好地管理这个问题提供一些建议吗?
哦顺便说一句,这是一个 Metro 应用程序,所以我们的 API 有限
这是伪代码:
public class ExposeSomeInterestingItems
{
private InitialisationState _initialised;
private readonly SemaphoreSlim _waiter =
new SemaphoreSlim(0);
public async Task StartInitialize()
{
if (_initialised == InitialisationState.Initialised)
{
throw new InvalidOperationException(
"Attempted to initialise ActiveTrackDown" +
"loads when it is already initialized");
}
_initialised =
InitialisationState.StartedInitialisation;
new TaskFactory().StartNew(async () =>
{
// This takes some time to load
this._interestingItems =
InterestingItemsLoader.LoadItems();
_waiter.Release();
_initialised = InitialisationState.Initialised;
});
}
public InterestingItem GetItem(string id) …Run Code Online (Sandbox Code Playgroud) 这可能是一个非常愚蠢的问题,但..
我有3个值,例如我们称之为minX,currentX和maxX.我正在寻找一种方法来计算currentX代表的maxX的百分比.例如,如果maxX为50且minX为40且currentX为45,那么我想得到50%这是非常基本的,但我遇到的问题是当我的一个或多个变量是负数时.
任何帮助将不胜感激,也让我知道,如果我没有充分解释自己
我正在尝试模拟具有内部重力的船舶/空间站。
为了实现这一点,我将玩家和飞船的所有内容都设为飞船的子级。飞船本身有对撞机,但没有刚体组件。这个想法是随着船的移动,它的所有内容也会移动。到目前为止非常简单。
为了模拟飞船中的重力,玩家控制器和所有刚体都关闭了默认重力。代替标准,每一帧都沿着母船的负向上向量施加一个力。
这种工作,但有一个主要问题我必须在这件事稳固之前解决。所有刚体都非常缓慢地在船内部滑动。
我知道这可能是由于地板的更新位置加上重力导致某种剪切力。物体总是逆着船的旋转而滑动。
我已经尝试过将物理材料的所有物理属性拖到质量上等等。这些都没有奏效,而且我很确定这是由于地板在移动的基本事实,即使 RB是地板所属对象的子项。
任何人都有解决方案,而不是某种胶带?我可以尝试使一切都成为动态的,并且仅在发生某些外部碰撞或其他事情时才“唤醒”,但这可能会变得非常麻烦。我需要它以尽可能多的通用方式工作。
一些代码:
在船上
void Update ()
{
transform.Rotate(new Vector3(Time.deltaTime * 0.125f,Time.deltaTime*0.5f,0));
}
void FixedUpdate()
{
Vector3 tempVec;
foreach(Rigidbody rb in rigidBodies)
{
//Gravity!!
tempVec = transform.up * -9.81f * rb.mass * Time.deltaTime;
rb.AddForce(tempVec, ForceMode.Acceleration);
}
}
Run Code Online (Sandbox Code Playgroud)
我还开发了一个版本,其中船跟随刚体的运动。我无法直接进行育儿,因此我必须简单地在每一帧手动设置变换以匹配物理代理。这仍然具有与上述相同的效果,尽管这可能是我最终想要移动船的方式,因为这将更恰当地与飞行机制联系起来。
.NET 4.0 ConditionalWeakTable<T>实际上是一个字典,其中字典的键被弱引用并且可以被收集,这正是我需要的.问题是我需要能够从这本字典中获取所有实时密钥,但MSDN声明:
它不包括字典通常具有的所有方法(例如GetEnumerator或Contains).
是否有可能从一个ConditionalWeakTable<T>?检索实时密钥或键值对?
通常我可以使用图像定位或云识别制作应用程序,但现在我需要使用物理建筑作为目标,所以我的问题是有谁知道如何通过vuforia + unity或vuforia android识别建筑物?
我在youtube上找到了一个应用程序,只是想知道它应该如何创建.
我们将从一个独立的故事开始,只是让你理解为什么:我想要处理任何针对同一个接口更改数据的操作:ICommand存在一些名为ICommandHandlers的东西,可以处理我想要的任何命令.所以,如果我想要一个CreatePersonCommand,我需要一个CreatePersonCommandHandler.
所以这是控制台应用程序的主体,它演示了这一点:(需要简单的注入器)
// The e.g. CreatePersonCommand, with TResult being Person, as an example.
public interface ICommand<TResult>
{
}
//This handles the command, so CreatePersonCommandHandler
public interface ICommandHandler<in TCommand, out TResult>
where TCommand : ICommand<TResult>
{
TResult Handle(TCommand command);
}
// Imagine a generic CRUD set of operations here where we pass
// in an instance of what we need made
public class CreateBaseCommand<TModel> : ICommand<TModel>
{
public TModel ItemToCreate { get; set; }
}
public class DeleteBaseCommand<TModel> : …Run Code Online (Sandbox Code Playgroud) c# generics dependency-injection inversion-of-control simple-injector
我正在尝试切换到Simple Injector Dependency Injection框架,因为我对它的速度印象深刻.
private static void RegisterServices(Container container)
{
container.RegisterPerWebRequest<IDbContext, DbContext1>();
////container.RegisterPerWebRequest<IDbContext, DbContext2>();
container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
container.RegisterPerWebRequest<IColourRepository, ColourRepository>();
Run Code Online (Sandbox Code Playgroud)
其中DbContext1和DbContext2继承自BaseDbContext类
public class BaseDbContext<TContext> : DbContext, IDbContext where TContext : DbContext
Run Code Online (Sandbox Code Playgroud)
它实现了一个相当简单的IDbContext接口(就像在SO上提供的许多接口),例如:
public interface IDbContext
{
IQueryable<TEntity> Find<TEntity>() where TEntity : class;
DbSet<TEntity> Set<TEntity>() where TEntity : class;
int SaveChanges();
void Dispose();
}
Run Code Online (Sandbox Code Playgroud)
如果我只使用一个DbContext类,它可以正常工作 - 存储库被注入,数据被拉出等.
但是,我还想在每个中使用带有少量DbSet的有界上下文(使用DDD有界上下文缩小EF模型),因为我的Code-First DbContext将包含数百个类
private static void RegisterServices(Container container)
{
container.RegisterPerWebRequest<IDbContext, DbContext1>();
container.RegisterPerWebRequest<IDbContext, DbContext2>();
container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
container.RegisterPerWebRequest<IColourRepository, ColourRepository>();
Run Code Online (Sandbox Code Playgroud)
然后我得到一个例外:
System.InvalidOperationException未由用户代码处理HResult = -2146233079消息=类型IDbContext已经注册,并且容器当前未配置为允许覆盖注册.要允许覆盖当前注册,请将Container.Options.AllowOverridingRegistrations设置为true.Source = …
c# dependency-injection ioc-container inversion-of-control light-inject
我正在尝试模仿Unity中的伺服工作,为我的项目 - 一个物理精确的人形机器人建模,我需要模拟一定程度上转动机器人的部分(腿,膝).我尝试使用铰链接头,但很快就变得明显,它使机器人变得晃动,有点不可预测.现在我正在尝试使用零投影(模式位置和角度)的可配置关节,但它有时会让机器人连续摇动他的部件.
如果我需要考虑每个部分的重量,我还有哪些其他选项可以使机器人或多或少真实(或者像在Microsoft机器人开发工作室中那样)?
在 Autofac 中,您可以使用 RegisterAssemblyTypes 注册您的依赖项,这样您就可以执行这样的操作,有没有办法在构建中执行类似DI的操作.net Core
builder.RegisterAssemblyTypes(Assembly.Load("SomeProject.Data"))
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)
这就是我要注册的内容
LeadService.cs
public class LeadService : ILeadService
{
private readonly ILeadTransDetailRepository _leadTransDetailRepository;
public LeadService(ILeadTransDetailRepository leadTransDetailRepository)
{
_leadTransDetailRepository = leadTransDetailRepository;
}
}
Run Code Online (Sandbox Code Playgroud)
LeadTransDetailRepository.cs
public class LeadTransDetailRepository : RepositoryBase<LeadTransDetail>,
ILeadTransDetailRepository
{
public LeadTransDetailRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory) { }
}
public interface ILeadTransDetailRepository : IRepository<LeadTransDetail> { }
Run Code Online (Sandbox Code Playgroud)
这就是我当时尝试注册的方式,但我无法弄清楚如何注册存储库 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddTransient<ILeadService, LeadService>();
//not sure how to register the repositories …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×2
physics ×2
android ×1
asp.net-core ×1
background ×1
generics ×1
light-inject ×1
math ×1
modeling ×1
rigid-bodies ×1
vuforia ×1