我的问题的前提,用简单的英语:
Foo 的库依赖于名为 Bar 的库FooBar仅依赖于 Foo考虑以下示例:
class Program
{
static void Main(string[] args)
{
Foo foo = Foo.Instance;
int id = foo.Id; // Compiler is happy
foo.DoWorkOnBar(); // Compiler is not happy
}
}
Foo定义如下
public class Foo : Bar
{
public new static Foo Instance { get => (Foo)Bar.Instance; }
public new int Id { get => Bar.Id; }
public void DoWorkOnBar()
{
Instance.DoWork();
}
}
Bar定义如下
public class Bar
{
public static Bar Instance { get => new Bar(); }
public static int Id { get => 5; }
public void DoWork() { }
}
完全难倒我的部分:
没有引用 Bar 库
FooBar 可以检索 Bar 提供的 ID(或者至少它编译)FooBar 不能请求 Foo 完成最终由 Bar与 foo.DoWorkOnBar(); 相关的编译器错误是
The type 'Bar' is defined in an assembly that is not referenced. You must add a reference to assembly 'Bar, Version 1.0.0.0, Culture=Neutral, PublicKeyToken=null' .
为什么编译器中似乎存在差异?
如果没有 FooBar 添加对 Bar 的引用,我会假设这些操作都不会编译。
最佳答案
首先,请注意 Foo.Id 和 Foo.DoWorkOnBar 的实现是不相关的;即使实现不访问 Bar,编译器也会以不同方式对待 foo.Id 和 foo.DoWorkOnBar():
// In class Foo:
public new int Id => 0;
public void DoWorkOnBar() { }
foo.Id 编译成功但 foo.DoWorkOnBar() 编译失败的原因是编译器使用不同的逻辑¹ 来查找属性和方法。
对于foo.Id,编译器首先在Foo中寻找名为Id的成员。当编译器发现 Foo 有一个名为 Id 的属性时,编译器会停止搜索并且不会去查看 Bar。编译器可以执行此优化,因为派生类中的属性会隐藏基类中具有相同名称的所有成员,因此 foo.Id 将始终引用 Foo.Id ,无论什么成员都可能在 Bar 中被命名为 Id。
对于foo.DoWorkOnBar(),编译器首先在Foo中寻找名为DoWorkOnBar的成员。当编译器发现 Foo 有一个名为 DoWorkOnBar 的方法时,编译器会继续在所有基类中搜索名为 DoWorkOnBar 的方法。编译器这样做是因为(与属性不同)方法可以重载,并且编译器实现重载决议算法的方式与 C# 规范中描述的方式基本相同:
Foo 及其基类 中声明的 DoWorkOnBar 的所有重载集组成。。第 1 步触发要求您添加对程序集 Bar 的引用。
C# 编译器能否以不同方式实现算法?根据 C# 规范:
The intuitive effect of the resolution rules described above is as follows: To locate the particular method invoked by a method invocation, start with the type indicated by the method invocation and proceed up the inheritance chain until at least one applicable, accessible, non-override method declaration is found. Then perform type inference and overload resolution on the set of applicable, accessible, non-override methods declared in that type and invoke the method thus selected.
所以在我看来,答案是"is":C# 编译器理论上可以看到 Foo 声明了一个适用的 DoWorkOnBar 方法,而不必费心查看 栏。然而,对于 Roslyn 编译器,这将涉及对编译器的成员查找和重载解析代码的重大重写——考虑到开发人员可以很容易地自行解决此错误,这样的努力可能不值得。
TL;DR — 当您调用方法时,编译器需要您引用基类程序集,因为这是编译器的实现方式。
¹ 请参阅 Microsoft.CodeAnalysis.CSharp.Binder class 的 LookupMembersInClass 方法.
² 请参阅 Microsoft.CodeAnalysis.CSharp.OverloadResolution class 的 PerformMemberOverloadResolution 方法.
关于c# - 为什么 C# 编译器可以 "see"静态属性,而不是实例方法,在未引用的 DLL 中的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51547053/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i