有没有办法通过反射执行"内部"代码?
这是一个示例程序:
using System;
using System.Reflection;
namespace ReflectionInternalTest
{
class Program
{
static void Main(string[] args)
{
Assembly asm = Assembly.GetExecutingAssembly();
// Call normally
new TestClass();
// Call with Reflection
asm.CreateInstance("ReflectionInternalTest.TestClass",
false,
BindingFlags.Default | BindingFlags.CreateInstance,
null,
null,
null,
null);
// Pause
Console.ReadLine();
}
}
class TestClass
{
internal TestClass()
{
Console.WriteLine("Test class instantiated");
}
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个测试类通常是完美的,但是当我尝试通过反射创建一个实例时,我得到一个missingMethodException错误,说它无法找到构造函数(如果你尝试从程序集外部调用它会发生什么).
这是不可能的,还是我可以做一些解决方法?
我想知道是否有一种有效的算法来找到N×N矩阵中最大的m个元素,方法头像这样:
double[] greatestValues(double[][] matrix, int numberOfElements);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正试图用iPhone/Touch SDK绘制一些简单的线条.我希望能够更改行的颜色,但调用CGContextSetRGBStrokeColor似乎不会影响使用CGContextAddLineToPoint创建的绘制线,直到实际调用CGContextStrokePath.因此,如果我进行多次调用以更改颜色,则只有在CGContextStrokePath之前生成的颜色才会生效.这就是我的意思:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 0, 0);
CGContextSetRGBStrokeColor(ctx,1,0,0,1);
CGContextAddLineToPoint(ctx, 100, 100);
//CGContextStrokePath(ctx);
CGContextSetRGBStrokeColor(ctx,0,1,0,1);
CGContextAddLineToPoint(ctx, 200, 300);
//CGContextStrokePath(ctx);
CGContextSetRGBStrokeColor(ctx,0,0,1,1);
CGContextStrokePath(ctx);
}
Run Code Online (Sandbox Code Playgroud)
我假设我正在做一些可怕的错误,我只是无法弄清楚是什么.我想如果我添加了CGContextStrokePath调用,那会有所帮助,但事实并非如此.
请参阅下面的讨论,了解我如何获得更正的代码:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 0, 0);
CGContextSetRGBStrokeColor(ctx,1,0,0,1);
CGContextAddLineToPoint(ctx, 100, 100);
CGContextStrokePath(ctx);
CGContextClosePath(ctx);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 100, 100);
CGContextSetRGBStrokeColor(ctx,0,1,0,1);
CGContextAddLineToPoint(ctx, 200, 300);
CGContextStrokePath(ctx);
}
Run Code Online (Sandbox Code Playgroud) 我有一个接受IEnumerable-decimals并执行各种数学函数的方法。我想对IEnumerable-int-使用相同的方法。我该如何实施?例如找到一个简单的总和?
void Calculate<T>(IEnumerable <T> ListOFNumbers)
{
int count= ListofNumbers.Count();
?sum=?;
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个单例类,这个类返回一个数据库连接.所以我的问题是这种联系也满足单身标准?
如果不是,我怎么能让它成为单身人士.
这是代码.
public sealed class SingletonDB
{
static readonly SingletonDB instance = new SingletonDB();
static SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static SingletonDB()
{
}
SingletonDB()
{
}
public static SingletonDB Instance
{
get
{
return instance;
}
}
public static SqlConnection GetDBConnection()
{
return con;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在做这个让我困惑的作业......
我提供了以下BinarySearchTree类
import java.util.NoSuchElementException;
/**
*
* @param <T> The type of data stored in the nodes of the tree, must implement Comparable<T> with the compareTo method.
*/
public class BinarySearchTree<T extends Comparable<T>> {
BinaryTree<T> tree;
int size;
public BinarySearchTree() {
tree = new BinaryTree<T>();
size = 0;
}
public boolean isEmpty() {
return tree.isEmpty();
}
protected BinaryTree<T> recursiveSearch(BinaryTree<T> root, T key) {
if (root == null) {
return null;
}
int c = key.compareTo(root.data);
if (c == 0) { …Run Code Online (Sandbox Code Playgroud) 我正在使用ruby中的文本编辑器,我需要支持使用用户提供的正则表达式模式的"查找"功能.这是一个简单(熟悉)的用例:
Joe User正在编辑文本文件,并将光标定位在文件中间的某个位置.他想从当前光标位置向后搜索与任意正则表达式匹配的最近子字符串.
我认为这个问题相当于将用户的模式应用于文件中光标位置之前的整个字符串.当然,我可以从文件的开头循环遍历所有匹配并使用最后一个匹配,但这看起来非常低效......最好搜索"从右到左",但我还没有找到办法这与ruby Regexp.你能帮我吗?
我想用符合W3C标准的CSS来定位IE7和IE8.有时为一个版本修复CSS并不能解决另一个版本.我怎样才能做到这一点?
我可以使用mshtml完成我需要做的大部分工作,但是我对如何将复选框输入元素设置为"已检查"感到困惑.这是情况......
IHTMLElementCollection inputElements = (IHTMLElementCollection)doc.all.tags("input");
foreach (IHTMLElement el in inputElements)
{
string elementHtml = el.outerHTML;
string termsOfServiceIdentifier = "id=chkUTOS_ver2";
// select the Terms of Service checkbox
if (elementHtml.Contains(termsOfServiceIdentifier))
{
HTMLInputElement chkTOS = (HTMLInputElement)el;
chkTOS.@checked = true; // that's the solution. Thanks Wayne.
}
else
{
// do nothing - we're not interested in this element
}
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助!
格雷格
我试图让这个Zend Validator输出一个转到resetpass表单的链接.目前,它只是将HTML作为文本输出.有关如何将其作为HTML写入页面的任何想法?
谢谢!
这是我的代码:
protected $_authAdapter;
protected $_messageTemplates = array(
self::NOT_UNIQUE => 'This email has already been registered! <a href=\'/user/resetpass/\'>Need to reset your password?</a>'
);
public function isValid($value, $context=null)
{
$value = (string) $value;
$users = new Users(array('db' => 'tdb'));
if($users->userExists($value)){
$this->_error(self::NOT_UNIQUE);
return false;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)