我是一个Unity noob,我有一个问题.我想在每个IPerson实例中注入一个IAgeCalculator,以便IAgeCalculator实例可供我以后创建的任何IPerson使用.
这是我到目前为止所尝试的.它有效,但感觉不对.
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.RegisterType<IAgeCalculator, AgeInYearsCalculator>("Years");
container.RegisterType<IAgeCalculator, AgeInDaysCalculator>("Days");
// Using the initializer like this does not feel right,
// but I cant think of another way...
var ageCalculator = container.Resolve<IAgeCalculator>("Days");
var personInitializer = new InjectionMethod("Initializer", ageCalculator);
container.RegisterType<IPerson, Person>(personInitializer);
var person1 = Factory<IPerson>.Create(container);
person1.Name = "Jacob";
person1.Gender = "Male";
person1.Birthday = new DateTime(1995, 4, 1);
var person2 = Factory<IPerson>.Create(container);
person2.Name = "Emily";
person2.Gender = "Female";
person2.Birthday = new DateTime(1998, 10, 31);
} …Run Code Online (Sandbox Code Playgroud) 这个问题与史蒂文的答案有关 - 这里.他提出了一个非常好的记录器包装器.我将在下面粘贴他的代码:
public interface ILogger
{
void Log(LogEntry entry);
}
public static class LoggerExtensions
{
public static void Log(this ILogger logger, string message)
{
logger.Log(new LogEntry(LoggingEventType.Information,
message, null));
}
public static void Log(this ILogger logger, Exception exception)
{
logger.Log(new LogEntry(LoggingEventType.Error,
exception.Message, exception));
}
// More methods here.
}
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是创建代理Microsoft.Extensions.Logging的实现的正确方法是什么,以及在代码中稍后使用它的最佳方法是什么?
使用MVC 5和Razor 3我正在尝试HTML使用剃刀语法动态创建属性名称,特别是data-*属性.
因此,这是对名称的属性,并没有对价值.
<div data-foo="bar">
属性名称:data-foo
属性值:bar
这就是我正在尝试使用Razor语法:
<div data-search-@Model.Name="@view.Name">
<div data-search-@(Model.Name)="@view.Name">
Run Code Online (Sandbox Code Playgroud)
Razor都没有识别这两个例子,而是按原样呈现.所以html属性名称输出字面意思是:data-search-@Model.Name.
我在这里运气不好吗?
自从关注DI和TDD之后,我对何时应该创建私有方法感到困惑.您能否告诉我在制定方法私有保持可测试性和依赖注入时,应该考虑哪些经验法则?
我相信一个例子可能会有所帮助:
假设我有一个包含3个方法的接口,如下所示:
public interface IWordFrequencyAnalyzer
{
int CalculateHighestFrequency(string forText);
int CalculateFrequencyForWord(string text, string word);
IList<IWordFrequency> CalculateMostFrequentNWords(
string text, int n);
}
Run Code Online (Sandbox Code Playgroud)
现在,我可以编写一个类,它可以实现一个私有方法,它接受一个字符串,并可以计算其中的单词的频率,然后在每个公共方法中,我可以根据它的要求进行操作.在这种情况下,我将是能够测试合同.
要么
我可以将私有方法提取到单独的类中,例如WordProcessor,它实现IWordProcessor,使用单个公共方法将句子拆分为单词并将其作为依赖项传递给IWordFrequencyAnalyzer的实现.这样,分割单词的实现也是可测试的.
你会建议采用哪种方法?
谢谢,-Mike
我正在测试Ninject并试图了解如何将存储库注入到单例类中.下面是工作存储库和单例类示例...
public interface ITestRepository
{
void TestRepositoryMethod();
}
public class TestRepository:ITestRepository
{
public void TestRepositoryMethod()
{
}
}
public class TestSingletonInjectionClass
{
private readonly ITestRepository _repository;
public TestSingletonInjectionClass(
ITestRepository repository)
{
_repository = repository;
}
public void TestMethod()
{
}
}
Run Code Online (Sandbox Code Playgroud)
通过成功的测试方法
[TestMethod]
public void SimpleTestSingleton()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<ITestRepository>().To<TestRepository>();
var testSingletonInjectionClass =
kernel.Get<TestSingletonInjectionClass>();\
Assert.IsNotNull(testSingletonInjectionClass);
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个问题
1.这是获取单例类实例的正确方法吗?
kernel.Get<TestSingletonInjectionClass>()
Run Code Online (Sandbox Code Playgroud)
2.如何从应用程序代码中获取单例实例类.在测试方法中,我创建Ninject内核并访问Get方法.如何访问ninject内核表单代码?
我正在尝试使用Unity Configuration向类属性添加依赖项,并且我尝试注入的类型也是通用的.
我有界面
public interface ISendMessage
{
void Send(string contact, string message);
}
Run Code Online (Sandbox Code Playgroud)
类
public class EmailService : ISendMessage
{
public void Send(string contact, string message)
{
// do
}
}
Run Code Online (Sandbox Code Playgroud)
类
public class MessageService<T> where T : ISendMessage
{
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过构造函数注入在其他类中使用它
public MyService(MessageService<ISendMessage> messageService)
{
}
Run Code Online (Sandbox Code Playgroud)
我怎么注射MessageService<EmailService>而不是MessageService<ISendMessage>?
我试着通过app.config来做
<alias alias="MessageService'1" type="MyNamespace.MessageService'1, MyAssembly" />
<alias alias="EmailMessageService'1" type="MyNamespace.MessageService'1[[MyNamespace.EmailService, MyAssembly]], MyAssembly" />
Run Code Online (Sandbox Code Playgroud)
我收到错误
无法解析类型名称或别名MessageService'1.请检查配置文件并验证此类型名称.
我如何通过MessageService<T>实现参数MessageService<EmailService>?
谢谢
更新
我将我的课修改为以下内容:
public class MessageService<T> where T : …Run Code Online (Sandbox Code Playgroud) 所以......我的目标如下:
但是,当我尝试写入StreamingAssets文件夹时,我遇到错误,位于以下路径:
jar:file:///data/app/com.catlard.testName-1.apk!/assets/ash.mp4
Run Code Online (Sandbox Code Playgroud)
这也是下面PlaceMovieInStreamingAssets函数中_debugString的第一个值.甚至可以在Android上运行时写入StreamingAssets文件夹吗?这是我用来做这些东西的课程.它在WriteAllBytes调用中停止 - 我知道,因为我在前后放置了print语句,而这就是它停止的地方.
using UnityEngine;
using System.Collections;
using System.IO;
public class StartVideoFromWeb : MonoBehaviour {
public string _movieFileName;
public string _movieHTTPLocation;
private string _debugString;
private MediaPlayerCtrl _control;
public float _mbFileSize = 16.7f;
private string _movieLocationOnDevice;
private float _prevProg;
private WWW _movieWWW;
public float _kbSpeed;
public float _timeBetweenSpeedMeasurements = 1f;
// Use this for initialization
public IEnumerator Start() {
yield return StartCoroutine("LoadMovie");
PlaceMovieInStreamingAssets(_movieWWW);
yield return new WaitForSeconds(.5f);
//PlayMovieInCtrl(_movieHTTPLocation + _movieFileName);
PlayMovieInCtrl(_movieLocationOnDevice);
yield return 0; …Run Code Online (Sandbox Code Playgroud) Hello Stack Overflow社区.
我刚刚开始使用Unity将我的视频游戏移植到多个平台.我有一个关于在Unity中以编程方式创建对象的问题.这就是我目前的游戏:

当用户点击相机按钮时,相机图片在Tap和offTap上缩放得更大.我希望整个屏幕只闪一分钟,但我不知道如何做到这一点.这是我已经针对这个问题的C#代码:
using UnityEngine;
using System.Collections;
public class question3 : MonoBehaviour {
int cameraTaps = 0;
// Use this for initialization
void Start () {
}
IEnumerator CameraCoroutine() {
Debug.Log("Before Waiting 3 seconds");
yield return new WaitForSeconds(3);
Debug.Log("After Waiting 3 Seconds");
Application.LoadLevel("question4");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.name == "camera")
{
var camera …Run Code Online (Sandbox Code Playgroud) 如下代码:
Debug.LogWarning("updating scale fix, scalefactor: "+scaleFactor+" - Current scale is: "+cell.transform.localScale.x);
cell.transform.localScale.Set (scaleFactor,scaleFactor,scaleFactor);
Debug.LogWarning("Scale after fix: " + cell.transform.localScale.x);
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
updating scale fix, scalefactor: 0.9 - Current scale is 0.8921105
UnityEngine.Debug:LogWarning(Object)
Scale after fix: 0.8921105
UnityEngine.Debug:LogWarning(Object)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我只是假设由于这些事情是紧接着发生的,因此应该更新规模。还是在框架完成之后发生?
任何帮助表示赞赏。
如何使用SetActiveRecursively(Moment = 1秒)在Unity中创建闪烁对象.
我的例子(改变):
public GameObject flashing_Label;
private float timer;
void Update() {
while(true)
{
flashing_Label.SetActiveRecursively(true);
timer = Time.deltaTime;
if(timer > 1)
{
flashing_Label.SetActiveRecursively(false);
timer = 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)