我正在使用C#并尝试输出几行到ASCII文件.我遇到的问题是我的Linux主机看到这些文件:
ASCII text, with CRLF line terminators
Run Code Online (Sandbox Code Playgroud)
我需要这个文件只是:
ASCII text
Run Code Online (Sandbox Code Playgroud)
CRLF引起了一些问题,我希望在C#中有一种方法可以创建以我想要的方式格式化的文件.
我基本上使用这段代码:
string[] lines = { "Line1", "Line2" };
File.WriteAllLines(myOutputFile, lines, System.Text.Encoding.UTF8);
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法来创建没有CRLF行终止符的文件?我可以在Linux方面处理它,但宁愿从一开始就以适当的格式创建文件.
我对XSD生成器有一种奇怪的行为,我无法解释.我得到了这样的XSD:
<xs:complexType name="StageSequenceElement" mixed="false">
<xs:complexContent>
<xs:extension base="CoreObject">
<xs:sequence>
<xs:element name="Description" type="xs:string" minOccurs="0">
<xs:annotation>
<xs:documentation>Some Doc</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="StageRef" type="ObjectReference">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="MinDuration_100ms" type="xs:int" nillable="true" minOccurs="0">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="MaxDuration_100ms" type="xs:int" nillable="true">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="StageOnDemand" type="xs:boolean" nillable="true" minOccurs="0">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
它源自CoreObject:
<xs:complexType name="CoreObject">
<xs:sequence>
<xs:element name="No" type="xs:int">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
这只是XSD的一小部分,还有更复杂的类型.
因此,当我生成类似于此的类时,我得到一个生成的类,它有两个属性(除了我期望的5):
public bool MinDuration_100msSpecified
Run Code Online (Sandbox Code Playgroud)
和
public bool StageOnDemandSpecified
Run Code Online (Sandbox Code Playgroud)
所以对于"原始"属性,附加了"Specified",类型现在是bool.任何人都可以解释为什么会这样吗?
客户端可以使用bindService()/ 调用原始绑定/解除绑定服务unbindService().
我的问题是如何在服务端取消绑定服务,而不是unbindService()客户端调用,可能我应该调用它unbindClient.
我认为服务应该知道哪些客户端绑定了它,那么有没有办法告诉服务取消绑定特定客户端?
因为我只写服务,我不知道客户端unbindService()是否正确调用,所以我有这个问题..
作为对此的后续问题
public interface IFeature { }
public class FeatureA : IFeature { }
IFeature a = new FeatureA();
Activate(a);
private static void Activate<TFeature>(TFeature featureDefinition) where TFeature : IFeature
{
}
Run Code Online (Sandbox Code Playgroud)
我没有理解,一旦将FeatureA其转换IFeature为泛型方法,它将始终IFeature作为类型参数获取.
我们有一项服务,为我们提供了一个列表功能(List<IFeature>).如果我们想迭代这些特性,在泛型方法中传递每个特征,我想没有办法在泛型方法中获得具体类型而不是
由于反射非常昂贵,我想使用动态强制转换.以这种方式调用方法有什么缺点吗?不知怎的,我这样做时感觉很脏:-)
我想知道如果我在派生类上实现一个接口,如果基抽象类实现它已经是抽象的,是否有任何(运行时)差异:
public interface IFoo
{
void Bar();
}
public abstract class Base : IFoo
{
public abstract void Bar();
}
public class Derived : Base, IFoo // does this make any difference?
{
public override void Bar()
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
例如,当我检查实例是否在运行时实现接口等时,IFoo编写“实现”有什么区别吗?Derived
就像标题所说:有没有办法改变VS用来创建设计器文件的模板?例如控件或其事件的命名约定.我做了一些研究,但只能找到一些较旧的帖子,基本上说"不是不是",也许在过去几个月左右有些变化......
我能够识别用于创建类的默认模板的文件夹,该模板在开头"C:\ Program Files(x86)\ Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Windows Forms\1033"中创建.但我仍然在寻找生成事件方法等的模板......
编辑:对不起,如果我混淆了我的问题.正如Basti所说,我确实寻找两者,更改类模板(你可以通过改变模板文件,在VS目录C:\ Program Files(x86)\ Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates中找到)并且还更改了生成字段,事件名称等的命名约定.
这个问题已经在SO上以一种或另一种方式提出,但不是这样的.我刚刚找到了一个非常基本的问题,我正在寻找一个令人满意的解决方案:-)我得到了一个具有两个整数属性的对象列表.现在我想找到列表中所有对象的两个属性的最大值.
我想出了三个解决方案:
第一种方法:
int max = Math.Max(list.Max(elem => elem.Nr), list.Max(elem => elem.OtherNr));
Run Code Online (Sandbox Code Playgroud)
第二种方法:
public int Max(List<Thing> list)
{
int maxNr = 0;
foreach (var elem in list)
{
if (elem.Nr > maxNr)
maxNr = elem.Nr;
if (elem.OtherNr > maxNr)
maxNr = elem.OtherNr;
}
return maxNr;
}
Run Code Online (Sandbox Code Playgroud)
第三种方法是通过两个属性进行排序,然后只取第一个条目并获得一个或另一个属性.
我想找到最快的方法来做到这一点.因此,在所有方法中,我喜欢第二个帖子(从性能的角度来看).即使第一个较短,您也必须两次查看列表.
还有其他方法吗?
我想做这样的事情:
public static void Initialize<T>(T obj) where T : BaseClass
{
SetDefault(obj);
}
private static void SetDefault(AInheritedFromBaseClass thing)
{
// do something
}
private static void SetDefault(BInheritedFromBaseClass thing)
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
因此,每当我初始化obj时,它都会指向正确的方法.那可能吗?
我无法在自己的类上实现这些方法,因为它们是外部类.所以基本上我希望有一种gereric方式来按照我想要的方式初始化它们.我想避免这样的事情:
if (obj is TypeA)
{
ClassThis();
} else if (obj is TypeB)
{
CallThat();
}
//etc.
Run Code Online (Sandbox Code Playgroud) 我有以下相当简单的代码
<Window ... Width=400 Height=400>
<ScrollViewer HorizontalScrollBarVisibility="Auto" >
<StackPanel VerticalAlignment="Top"
HorizontalAlignment="Left">
<TextBox TextWrapping="Wrap"
Margin="0,5,0,5"
Width="500"
Padding="20">Scrolling is enabled when it is necessary.
Resize the window, making it larger and smaller.</TextBox>
<StackPanel Orientation="Horizontal">
<Label Content="aswkognweklng"></Label>
<TextBox TextWrapping="Wrap"
Margin="0,5,0,5"
Width="500"
Padding="20">Scrolling is enabled when it is necessary.
Resize the window, making it larger and smaller.</TextBox>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Window>
Run Code Online (Sandbox Code Playgroud)
我想禁用以下行为:
=> 滚动查看器将移动滚动条,使文本框左边框与可见窗口边框对齐
我想禁用这种自动滚动行为。那可能吗?
用户交互的默认滚动行为应该仍然有效。因此,当用户与滚动条交互时,它应该正常滚动内容。
c# ×7
interface ×2
android ×1
bind ×1
dynamic ×1
file-io ×1
generics ×1
inheritance ×1
line-endings ×1
linq ×1
list ×1
max ×1
mstest ×1
reflection ×1
resharper ×1
scrollviewer ×1
service ×1
unbind ×1
unit-testing ×1
wpf ×1
xsd.exe ×1