在C#中可以通过MSDN文档知道某些方法/属性/字段可以或不能返回空值吗?
例如来自MSDN的Image.RawFormat属性说:
ImageFormat表示此Image的文件格式.
它可以返回null吗?我应该在我的代码中执行这样的空检查还是总是不为空?
请看我的代码:
Graphics grfx = Graphics.FromImage(new Bitmap(1, 1));
System.Drawing.Font f = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
const string text1 = "check_space";
SizeF bounds1 = grfx.MeasureString(text1, f);
const string text2 = "check_space ";
SizeF bounds2 = grfx.MeasureString(text2, f);
Assert.IsTrue(bounds1.Width < bounds2.Width); // I have Fail here!
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我的测试失败了.为什么文本与尾部的空间是不是由宽度大于内容更加没有空间?
更新:我可以理解这两个字符串不相等.但是,我在心理上理解带空格的字符串应该比没有空格的字符串宽.别?
看我的Ruby代码:
h=Hash.new([])
h[0]=:word1
h[1]=h[1]<<:word2
h[2]=h[2]<<:word3
print "\nHash = "
print h
Run Code Online (Sandbox Code Playgroud)
输出是:
Hash = {0=>:word1, 1=>[:word2, :word3], 2=>[:word2, :word3]}
Run Code Online (Sandbox Code Playgroud)
我原以为有
Hash = {0=>:word1, 1=>[:word2], 2=>[:word3]}
Run Code Online (Sandbox Code Playgroud)
为什么附加第二个哈希元素(数组)?
如何用新数组元素追加第3个哈希的元素?
我想找到正确的方法来计算C#中指定字体的文本宽度.我在Java中有以下方法,它似乎有效:
public static float textWidth(String text, Font font) {
// define context used for determining glyph metrics.
BufferedImage bufImage = new BufferedImage(2 /* dummy */, 2 /* dummy */, BufferedImage.TYPE_4BYTE_ABGR_PRE);
Graphics2D g2d = (Graphics2D) bufImage.createGraphics();
FontRenderContext fontRenderContext = g2d.getFontRenderContext();
// determine width
Rectangle2D bounds = font.createGlyphVector(fontRenderContext, text).getLogicalBounds();
return (float) bounds.getWidth();
}
Run Code Online (Sandbox Code Playgroud)
但是在C#中观察我的代码:
public static float TextWidth(string text, Font f)
{
// define context used for determining glyph metrics.
Bitmap bitmap = new Bitmap(1, 1);
Graphics grfx = Graphics.FromImage(bitmap);
// determine …Run Code Online (Sandbox Code Playgroud) 我遵循项目结构
/build-out/MyApp.dll
/dependencies/ResFile.xml
/src/MyFile.cs
在MyFile.cs我想开我的ResFile.xml是在/依赖目录,阅读它的一些需求。一切都像Visual Studio 中的魅力一样,但是当我制作一个dll并将它与另一个应用程序(作为外部库)一起使用时,我收到一个错误,因为它找不到依赖项/ResFile.xml文件。
那么,如何将资源文件添加到结果MyApp.dll文件中呢?
是否可以在标记为Spring的@Transactional的方法中执行提交?
@PersistenceContext
private EntityManager em;
@Transactional(propagation = Propagation.REQUIRED)
public void saveMembersWithMultipleCommits(List<Member> members)
throws HibernateException
{
Iterator<Member> it = members.iterator();
while (it.hasNext())
{
while (it.hasNext())
{
Member wsBean = it.next();
em.persist(wsBean); // overall commit will be made after method exit
log.info("Webservices record " + wsBean + " saved. " + i++);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想在说出每500个项目之后提交DB.这可能与上述背景有关吗?
在C#中可以格式化吗
4.52浮点数转4.52字符串
和
4.520浮点数到4.52字符串,即省略尾部零?
编辑:我想我没有强调真正的问题。我需要一种符合上述两个示例的模式!
看我的代码:
var ths = $("#my_table table th");
if (ths.length > 1) {
var $th = ths[1]; // tried here plain 'var th' - still got error
th.find('#map_column_element_id'); // error here!
}
Run Code Online (Sandbox Code Playgroud)
我得到了一些JQuery对象.然后我尝试将第二个元素作为JQuery对象进行管理.当我发出
th.find(...)
Run Code Online (Sandbox Code Playgroud)
我得到TypeError:th.find不是一个函数.我究竟做错了什么?
我是 Python 新手。尝试使用此模块https://pypi.org/project/sparql-client/
模块.py
from sparql import Service
class MyModule:
def my_method(self):
s = Service('https://my-endpoint:8182/sparql', "utf-8", "GET")
statement = """
MOVE uri:temp_graph TO uri:user_graph
ADD uri:temp_graph TO uri:user_graph
""".format(user_graph="http://aws.amazon.com/account-uid",
temp_graph="http://aws.amazon.com/account-uid-temp")
s.query(statement)
Run Code Online (Sandbox Code Playgroud)
我正在尝试测试它
测试模块.py
import unittest
from unittest.mock import patch, Mock
class TestModule(unittest.TestCase):
@patch('sparql.Service', autospec=True)
def test_mymethod(self, sparql_mock):
sparql_instance = sparql_mock.return_value
sparql_instance.query = Mock()
Run Code Online (Sandbox Code Playgroud)
跑步时我得到
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 1564, in <lambda>
getter = lambda: _importer(target)
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 1236, in _importer
thing = __import__(import_path)
File "/usr/local/lib/python3.9/site-packages/sparql.py", line 50, in <module> …Run Code Online (Sandbox Code Playgroud) 我是C#的新手.我来自Java世界.所以我对以下代码感到困惑:
class A
{
private PointF point;
public A(PointF point)
{
this.point = point;
}
public PointF Position
{
get { return point; }
}
}
Run Code Online (Sandbox Code Playgroud)
我想更改position属性的X坐标,所以我执行:
A a = new A(new PointF(1,2));
PointF p = a.Position;
p.X = 100;
Console.WriteLine(a.Position.X); // <--- I have 1 here!
Run Code Online (Sandbox Code Playgroud)
我想知道为什么输出不是100?据我了解,我已收到有关职位属性的私人领域的参考资料.我对吗?
我可以在不添加set-property的情况下更改属性并使用新的PointF对象传播Position吗?
我有我的CSS文件:
.my_table tr {
background:#BBBBBB;
}
Run Code Online (Sandbox Code Playgroud)
和
.my_table tr .hover_style {
background:#AAAAAA;
}
Run Code Online (Sandbox Code Playgroud)
在我的HTML中,我的表格中包含hover_style的所有行:
<table class='my_table'>
<tr class='hover_style'>
...
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
但是所有的行都有#BBBBBB颜色,但我预计会覆盖颜色#AAAAAA ;
我哪里错了?
将私有类成员更改为默认(包访问)以测试其行为是否是个好主意?我的意思是测试用例应该在测试目录中,但在与测试成员的类相同的包中.
编辑:你们所有人都说的是真的.但是类经常有辅助私有方法.而这些方法可能很复杂,因此需要进行测试.这太糟糕了 - 测试公共方法以确保正确处理私有复杂方法.你不这么认为吗?
我是JQuery的新手,无法在网上找到答案.
那么JQuery标准JS 警报功能中常用的替代方法是什么?
更新:也许我的问题有点不清楚.我知道Java Script的警报功能对于JQuery来说是可以的.我在JQuery上询问它的替代方案.我不喜欢在非常贫穷的能力警觉.我想使用更强大的东西,例如样式,按钮,标题等.所以我希望看到简单的JQuery的警报模拟调用.
c# ×6
java ×2
jquery ×2
.net ×1
arrays ×1
css ×1
dll ×1
formatting ×1
graphics ×1
hashtable ×1
hibernate ×1
html ×1
jpa ×1
jquery-ui ×1
measurement ×1
msdn ×1
null ×1
properties ×1
python ×1
resources ×1
ruby ×1
sparql ×1
spring ×1
transactions ×1
unit-testing ×1