我刚刚在这里阅读了关于在Fortran中使用模块的正确方法的非常好的问题/答案.通过在模块中编写子程序,除了澄清代码之外,还可以使它们显式化.
据我所知,模块必须放在一个文件中,例如"mod_exemple.f90".我编写的程序通常很长,许多子程序确实可以按目的排序,因此可以放在模块中.问题是:那会有很长的模块文件,有数百行.
你能把模块拆分成几个文件吗?是否建议?为什么?你有其他拆分的建议吗?
我正在使用Roslyn来分析C#代码,并且在使用明确实现的接口时遇到了一个问题.给定一个实现接口的类型,我无法按名称检索显式实现的成员.例如:
var tree = CSharpSyntaxTree.ParseText(@"
using System;
namespace ConsoleApplication1
{
class MyClass : IDisposable
{
void IDisposable.Dispose()
{
}
public void Dispose()
{
}
}
}");
var Mscorlib = new MetadataFileReference(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
var myType = compilation.GetTypeByMetadataName("ConsoleApplication1.MyClass");
var dispose = myType.GetMembers("Dispose").SingleOrDefault();
//explicitDispose is null.
var explicitDispose = myType.GetMembers("IDisposable.Dispose").SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
仅当类型存在于命名空间内时才会出现这种情况,以下代码可以正常工作.
var tree = CSharpSyntaxTree.ParseText(@"
class MyClass : IDisposable
{
void IDisposable.Dispose()
{
}
public void …Run Code Online (Sandbox Code Playgroud) 显式成员实施的当前指导原则建议:
一个很好的例子就是你想要实现IXmlSerializable接口.该的ReadXml和中WriteXML有望方法由XmlSerializer的调用,而不是通常由开发商直接调用.
当提供一种替代方法来明确访问您希望允许被覆盖的成员时,调用显式实现的成员似乎是有意义的,以避免代码重复.考虑以下:
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace Demo
{
/// <summary>
/// Demonstrates explicit implementation of the IXmlSerializable interface.
/// </summary>
[Serializable(), XmlRoot(ElementName = "foo")]
public class Foo : IXmlSerializable
{
//============================================================
// IXmlSerializable Implementation
//============================================================
#region GetSchema()
/// <summary>
/// Returns an <see cref="XmlSchema"/> that describes the XML representation of the object.
/// </summary>
/// <returns>
/// An <see cref="XmlSchema"/> that describes …Run Code Online (Sandbox Code Playgroud) 处理这种情况的正确方法是什么.我的F#类DogTree中有一个方法应该满足为两个接口实现Bark()方法的要求.
type ITree =
interface
abstract Bark : unit -> unit
abstract Grow : unit -> unit
end
type IDog =
interface
abstract Bark : unit -> unit
abstract ChaseCar : unit -> unit
end
type TreeDog =
// so the "and" syntax below doesn't work - what is the correct way to handle?
interface IDog and ITree with
member this.Bark() = printfn "Bark"
Run Code Online (Sandbox Code Playgroud) 我有一个界面:
public interface Profile
{
string Name { get; }
string Alias { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
实现的所有对象Profile都有 aName和 an Alias,但有些限制Alias使其始终与Name. 应用此限制的人可以这样实现Alias:
string Profile.Alias
{
get
{
return ((Profile)this).Name;
}
set { }
}
Run Code Online (Sandbox Code Playgroud)
由于this在显式接口实现的上下文中只能是类型,Profile并且我们知道它是通过Profile接口而不是包含类或它实现的任何其他接口访问的,为什么需要强制转换?
使用return this.Name;了此错误,吸气实施效果:
Type `ConcreteProfile' does not contain a definition for `Name' and no extension method `Name' of type `ConcreteProfile' could be found (are you missing a …Run Code Online (Sandbox Code Playgroud) 为什么List定义了这三个方法?
public Enumerator GetEnumerator()
=> new Enumerator(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator()
=> new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator()
=> new Enumerator(this);
Run Code Online (Sandbox Code Playgroud)
他们都在做同样的事情。仅仅拥有这个还不够:
public Enumerator GetEnumerator()
=> new Enumerator(this);
Run Code Online (Sandbox Code Playgroud) 我有以下情况:
我有一些ViewModel对象,其中一些实现了一个接口ISomeInterface,有些则没有。这些接口公开了一个名为SomeEnumeration(IEnumerable<T>)的属性。
例如:
public sealed class ViewModelA : ViewModelBase, ISomeInterface
{
// ...
IEnumerable<Foo> ISomeInterface.SomeEnumeration
{
get { ...; }
}
}
public sealed class ViewModelB : ViewModelBase
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我的XAML,到目前为止,已经被设计的方式,无论是的ViewModels的正好有我反对(即绑定属性PropertyA,PropertyB等等)。我还没有遇到过这样的情况,即我设置为的ViewModels上不存在我要绑定的属性DataContext。但是,现在,我将……并且它将针对显式实现的属性(我不确定这是否会对WPF绑定引擎产生任何影响)。
基本上,我的xaml如下所示:
<StackPanel
Visiblity="{Binding Path=SomeEnumeration, Converter={StaticResource AnyConverter}">
...
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我不确定这是否还能工作,因为:
DataContext属性都包含该属性(如果不包含,则应将其隐藏)...在这种情况下,我该怎么办?DataContext确实包含该属性的,它是显式实现的……您是否必须先强制转换?我正在开发一组相当复杂的接口,它允许定义具有特定结构的对象。通过接口和泛型,它还允许我定义每个元素中可用的接口组合。该结构映射了我们背景中的某些内容,因此目标是在代码中重新创建结构,以便更好地理解代码实际操作的内容。
结果如下:
static void Main(string[] args)
{
IComplexDefinition1 CD1 = null;
CD1.Access.Level1.ElementB.SomeStructrueMethod();
CD1.Access.Level1.ElementC.ActorAMethod1();
CD1.Access.Level1.ElementC.ActorBMethod2();
CD1.Access.Level1.ElementC.ActorCMethod1();
CD1.Access.Level2.ElementA.SomeOperationPoolMethod();
CD1.Access.Level2.ElementC.ActorAMethod2();
CD1.Access.Level2.ElementC.ActorBMethod1();
CD1.Access.Level2.ElementC.ActorCMethod2();
}
Run Code Online (Sandbox Code Playgroud)
现在,在实现具体类时,我在 Visual Studio 中单击了“自动实现”,它出现了一行我到目前为止还没有看到的代码(并且没想到会发生这种情况)。因此,我正在寻找它的工作原理以及我可以在哪里阅读/研究更多相关信息的见解。
具体实现的相关部分是:
public class ComplexDefinition : IComplexDefinition1,....
{
protected IActorComposition _actors;
protected SomeStructure _structure;
protected SomeOperationPool _operationPool;
public IElementsBC<IActorCompositionLevel2, SomeStructure> Level1 => this;
public IElementsAC<IActorCompositionLevel1, SomeOperationPool> Level2 => this;
public SomeStructure ElementB => _structure;
public SomeOperationPool ElementA => _operationPool;
public IActorCompositionLevel2 ElementC => _actors;
IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC => _actors;
}
Run Code Online (Sandbox Code Playgroud)
尤其是这两行让我感到困扰(尤其是最后一行):
public IActorCompositionLevel2 ElementC => _actors;
IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC …Run Code Online (Sandbox Code Playgroud) c# ×5
interface ×4
.net ×2
data-binding ×1
datacontext ×1
f# ×1
file ×1
fortran ×1
ienumerable ×1
ienumerator ×1
module ×1
roslyn ×1
wpf ×1
xml ×1