在C#中,有人可以这样做:
MyClass myInstance = new MyClass();
dynamic mydynamicInstance = myInstance;
Run Code Online (Sandbox Code Playgroud)
然后,调用一个方法,如:
//This method takes a MyClass argument and does something.
Caller.InvokeMethod(myDynamicInstance);
Run Code Online (Sandbox Code Playgroud)
现在,这将导致在运行时确定myInstance类型,如果它有效,Caller.InvokeMethod将正常调用.
现在,我的问题是,如果这被认为是一种不好的做法dynamic,特别是在以下情况下:
1)InvokeMethod使用内部反射实例化myDynamicInstance类型的另一个实例.
2)有一个抽象基类MyBaseClass和它的许多子类,包括MyBaseClass.如果我们InvokeMethod为所有这些派生类提供了许多重载方法,我们是否可以使用它来在运行时允许类型确定,然后通过方法重载(或对该类方法调用的后期绑定)进行适当的调用)?:
public abstract class MyBaseClass {/*...*/}
public class MyClass : MyBaseClass {/*...*/}
public class MyAnotherClass : MyBaseClass {/*...*/}
MyBaseClass myBaseClassRef = new MyClass();
dynamic myDynamicInstance = myBaseClassRef;
Caller.InvokeMethod(myDynamicInstance);
Run Code Online (Sandbox Code Playgroud) 我有以下类树:
public class A
{
public static object GetMe(SomeOtherClass something)
{
return something.Foo();
}
}
public class B:A
{
public static new object GetMe(SomeOtherClass something)
{
return something.Bar();
}
}
public class C:B
{
}
public class SomeOtherClass
{
}
Run Code Online (Sandbox Code Playgroud)
鉴于SomeOtherClass parameter = new SomeOtherClass())这工作:
typeof(B).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
Run Code Online (Sandbox Code Playgroud)
但是这个:
typeof(C).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
Run Code Online (Sandbox Code Playgroud)
抛出一个NullReferenceException,虽然我希望它会调用与上面完全相同的方法.
我试过几个绑定标志无济于事.有帮助吗?
我有一个带有私有静态方法的类,带有可选参数.如何通过Reflection从另一个类调用它?有一个类似的问题,但它没有解决静态方法或可选参数.
public class Foo {
private static void Bar(string key = "") {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我如何调用Foo.Bar("test")和Foo.Bar()(例如,不传递可选参数)?
在我的Web API中,POST操作方法在服务器上上传文件.
对于单元测试此方法,我需要创建一个HttpContext并将一个文件放在其请求中:
HttpContext.Current.Request.Files
Run Code Online (Sandbox Code Playgroud)
到目前为止,我正在使用此代码伪装HttpContext,它完美地运行:
HttpRequest request = new HttpRequest("", "http://localhost/", "");
HttpResponse response = new HttpResponse(new StringWriter());
HttpContext.Current = new HttpContext(request, response);
Run Code Online (Sandbox Code Playgroud)
请注意,我不想使用Moq或任何其他Mocking库.
我怎么能做到这一点?(多部分内容可能吗?)
谢谢
我正在为计算软件编写一个单元测试。在测试用例中,我使用了“ PrivateObject”来访问私有方法“ sendNumberToCalculation()”,但我得到了类型未找到的错误构造函数。
public class CalculationTest
{
[TestMethod]
public void sendNumberToCalculationTest()
{
// -- Act
PrivateObject obj = new PrivateObject(typeof(Calculation));
Tokenization token = new Tokenization("5*10-18/(3+19)");
PolishNotation polish = new PolishNotation(token.MathExpressionParser());
double expected = 49.19;
// -- Actual
double actual = Convert.ToDouble(obj.Invoke("sendNumberToCalculation", polish));
// -- Assert
Assert.AreEqual(expected, actual);
}
}
public class Calculation
{
private Tokenization token;
private PolishNotation polish;
private Stack<double> numbers = new Stack<double>();
private Stack<string> operators = new Stack<string>();
public Calculation(string expression)
{
token = new …Run Code Online (Sandbox Code Playgroud) 我完全陷入了反思问题,我认为这不是很大,但我没有找到任何解决方案.
public class myClass : myClassIF {
public myClass() { }
private void doSomething_A() {
//...
}
private void doSomething_B() {
//...
}
public void DecideAndCall(string identifier) {
string methodName = "doSomething_" + identifier;
MethodInfo mi = this.GetType().GetMethod(methodName); //here i got a NullReference??
//here should be the Invocation of the Method and so on...
}
}
Run Code Online (Sandbox Code Playgroud)
界面看起来像这样:
public interface myClassIF {
void DecideAndCall(string identifier);
}
Run Code Online (Sandbox Code Playgroud)
如果我调用GetMethod("...") - Method,我总是得到一个NullReference.我无法理解这一点,因为在这个项目的另一部分我以前做过这个.但是我在其他类型中使用Refelction而不是"this".
是否有可能在实际的instanciated对象中反映方法?我想我应该是,但我不知道怎么样......
非常感谢!奔奔
我有一些问题克隆一个对象hierarchie.它是用于建模应用程序的工具包,工具箱包含类实例作为原型.但是我很难克隆这些:)
以下代码显示了问题:
public abstract class Shape {
protected List<UIElement> elements;
private Canvas canvas;
...
public Canvas getCanvas() { ... };
}
public class MovableShape : Shape {
protected ... propertyA;
private ... propertyXY;
...
}
public abstract class AbstractLayout : MovableShape, ... {
...
}
public class SomeLayoutClass : AbstractLayout, ... {
...
}
public class AContainingClass {
SomeLayoutClass Layout { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
当我将一个对象插入AContainingClass到我的项目工作表中时,它应该被克隆.到目前为止,我尝试了手动克隆(由于private基类中的字段而失败)和二进制序列化(BinaryFormatter和MemoryStreams).
第一种方法缺乏调用base.clone()方法的方法(或者我错了?),后者不起作用,因为UIElements不是 …
在阅读本网站上关于该主题的几个主题后,我得出以下结论:
要测试私有方法,有两个选择:
使用PrivateObject但是在进行大量测试时,VStudio 2012的测试工具很糟糕,建议使用NUnit但是PrivateObject使用的命名空间与NUnit for Asserts的命名空间相冲突,因此,应该避免使用.
转换受保护成员(属性+方法)中的所有私有成员,并使包装类继承测试的类并通过公共方法调用受保护的方法.
第二个选择是有效的但是在我看来,由于测试驱动的原因而打破OO封装非常尴尬.
我不是要讨论是否应该测试私有方法(在其他线程中已经讨论过),或者使用其他工具,或者保护VStudio(idem).
我宁愿听到你关于牺牲OO原则而不是可测试性的意见.
谢谢.
如何从一种形式访问私有方法到另一种形式?
例如,我在Form1中有这个方法:
Form1中:
private void Test (){}
Run Code Online (Sandbox Code Playgroud)
那么如何在Form2中访问该方法(private void Test),以便我在Form2中输入的值将在方法Test中发送?
Test是一个datagridview,在表单2中,我必须在其中输入带有相应值的Name,如果我按下save按钮,它应该自动保存在Form1中的datagridview中.
我一直在努力编写一个 double,例如82.0使用 Utf8JsonWriter。
默认情况下,methodWriteNumberValue方法接受一个 double 并为我格式化它,并且格式(这是标准的“G”格式)省略了“.0”后缀。我找不到控制它的方法。
按照设计,我似乎不能只将原始字符串写入 Utf8JsonWriter,但我找到了一种解决方法:创建一个 JsonElement 并调用 JsonElement.WriteTo`。这会调用Utf8JsonWriter 中的私有方法并将字符串直接写入其中。
有了这个发现,我做出了一个非常笨拙且效率低下的实现。
open System.Text.Json
void writeFloat(Utf8JsonWriter w, double d) {
String floatStr = f.ToString("0.0################")
JsonElement jse = JsonDocument.Parse(floatStr).RootElement
jse.WriteTo(w)
}
Run Code Online (Sandbox Code Playgroud)
无论如何我都需要格式化一个double,这样很好,但是解析它,创建一个jsonDocument和一个JsonElement,只是为了能够找到一种调用受保护方法的方法,似乎真的很浪费。但是,它确实有效(我用 F# 编写并转换为 C#,如果我在语法中犯了错误,请道歉)。
有没有更好的办法?想到的一些潜在解决方案(我是 dotnet 的新手,所以我不确定这里有什么可能):
至于为什么这是必要的:我需要强制写入整数值,.0因为我需要与之交互的非常具体的格式,它区分整数和浮点 JSON 值。(我对指数格式没意见,因为这显然是一个浮点数)。
如果我们继承一个类,私有变量也会继承吗?
我知道" 是的,变量是继承的,但是不能通过类接口直接访问. "
我想知道的是我们如何从子类访问私有变量/方法
我想说的是私人成员也是继承的.但是如何在不对其进行保护的情况下访问它们.