Get executing assembly name from referenced DLL in C#

What is the best way to get the application name (ie MyApplication.exe) of the executing assembly from a referenced class library in C#? I need to open the application's app.config to retrieve some appSettings variables for the referenced DLL. If you want to get the current appdomain's config file, then all you need to do is: ConfigurationManager.AppSettings .... (this requires a

在C#中从引用的DLL执行程序集名称

从C#中引用的类库中获取正在执行的程序集的应用程序名称(即MyApplication.exe)的最佳方法是什么? 我需要打开应用程序的app.config来检索引用的DLL的一些appSettings变量。 如果你想获得当前的appdomain的配置文件,那么你所需要做的就是: ConfigurationManager.AppSettings .... (这当然需要参考System.Configuration)。 要回答你的问题,你可以这样做,因为雷说(或使用Assembly.GetExecutingAssembly().FullN

EventInfo access modifiers

I am stuck now at method to retrieve access modifiers of EventInfo object (reflected event field in C# .NET). By access modifiers I mean: public/private/protected/internal and static, readonly etc. In theory (at the IL level) there are basically three members making up the event: add remove raise You can access each of those via a separate EventInfo property ( AddMethod , RemoveMethod

EventInfo访问修饰符

我现在停留在检索EventInfo对象的访问修饰符的方法(C#.NET中的反映事件字段)。 通过访问修饰符我的意思是:公共/私人/保护/内部和静态,只读等。 理论上(在IL级)基本上有三名成员组成这个事件: 加 去掉 提高 您可以通过单独的EventInfo属性( AddMethod , RemoveMethod , RaiseMethod )访问每个属性并检查每个属性的访问修饰符。 对于在C#中声明的事件,我希望没有提升方法,并且添加/删除方法具有相同的

Why And how to use static readonly modifier in C#

Well I was been studying about Destructor, which influenced me to constructors once again ... So started some googling and testing than I faced something like this .. public class Teacher { private static DateTime _staticDateTime; private readonly DateTime _readOnlyDateTime; /*Resharper telling me to name it StaticReadolyDateTime insted of _staticReadolyDateTime*/ private static

为什么以及如何在C#中使用静态只读修饰符

那么我一直在研究关于析构函数,这又一次影响到了构造函数......所以开始一些Google搜索和测试,而不是我面对这样的事情。 public class Teacher { private static DateTime _staticDateTime; private readonly DateTime _readOnlyDateTime; /*Resharper telling me to name it StaticReadolyDateTime insted of _staticReadolyDateTime*/ private static readonly DateTime StaticReadolyDateTime; static

How to define the constants the used also by the subclass?

I need to define some constants that will be used by the base class and its sub class. Not sure what is the correct way to define them. I understand the differences of const, readonly, static const, as well as public, protected, and private (while I seldom see "protected" is used in C#). How these constant should be defined? should them be public const, or public readonly, or priva

如何定义由子类使用的常量?

我需要定义一些将被基类及其子类使用的常量。 不知道什么是定义它们的正确方法。 我了解常量,只读,静态常量,以及公共,受保护和私有的差异(虽然我很少在C#中使用“protected”)。 应如何定义这些常数? 他们应该是公共常量,或公共只读,或私人常量,或私人只读,并使用公共getter / setter的子类使用,或者他们应该被定义为受保护的? 另一个问题是关于BaseClass中的变量FilePath。 FilePath将被BaseClass中的一些

Protected readonly field vs protected property

I have an abstract class and I'd like to initialize a readonly field in its protected constructor. I'd like this readonly field to be available in derived classes. Following my habit of making all fields private and exposing properties, I implemented this as follows: abstract class Foo { private readonly int _field; protected Foo(int field) { _field = field; }

受保护的只读字段与受保护的属性

我有一个抽象类,我想在其受保护的构造函数中初始化只读字段。 我希望这个只读字段在派生类中可用。 遵循我将所有领域设为私有并暴露属性的习惯,我实现了这一点,如下所示: abstract class Foo { private readonly int _field; protected Foo(int field) { _field = field; } protected int Field { get { return _field; } } } 但后来我想知道在这里保持私密性是否真的有

const, readonly and mutable value types

I'm continuing my study of C# and the language specification and Here goes another behavior that I don't quite understand: The C# Language Specification clearly states the following in section 10.4: The type specified in a constant declaration must be sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, an enum-type, or a reference-type. It

const,只读和可变值类型

我正在继续研究C#和语言规范,这里有另外一个我不太明白的行为: C#语言规范在第10.4节中明确说明了以下内容: 常量声明中指定的类型必须是sbyte,byte,short,ushort,int,uint,long,ulong,char,float,double,decimal,bool,string,枚举类型或引用类型。 它还在第4.1.4节中陈述了以下内容: 通过常量声明,可以声明简单类型的常量(第10.4节)。 不可能有其他结构类型的常量,但类似的效果由静态只读字段

Why do I NOT get warnings about uninitialized readonly fields?

The C# compiler is kind enough to give you a "field is never assigned to" warning if you forget to initialize a readonly member which is private or internal, or if the class in which it is being declared is internal. But if the class is public, and the readonly member is public, protected or protected internal, then no warning for you! Does anyone know why? Sample code which demons

为什么我不会收到有关未初始化的只读字段的警告?

如果您忘记初始化一个只读或者私有的只读成员,或者它所声明的类是内部的,那么C#编译器就足以给你一个“从未分配过的字段”警告。 但是,如果班级是公开的,并且只读成员是公开的,受保护的或者内部受保护的,那么不会有任何警告! 有谁知道为什么? 示例代码说明发出警告的条件以及未发出警告的条件: namespace Test1 { class Test1 { #if TRY_IT public readonly int m; //OK: warning CS0649: Fiel

Why does the lock object have to be static?

It is very common to use a private static readonly object for locking in multi threading. I understand that private reduces the entry points to the locking object by tightening the encapsulation and therefore access to the most essential. But why static? private static readonly object Locker = new object(); At the end the field is only used within my class only, and I could also just use thi

为什么锁对象必须是静态的?

使用私有静态只读对象锁定多线程是非常常见的。 我明白私人通过收紧封装来减少锁定对象的入口点,因此可以访问最基本的内容。 但为什么静态? private static readonly object Locker = new object(); 最后,该字段仅在我的课程中使用,我也可以使用它来代替: private readonly object Locker = new object(); 任何意见? 更新: 作为一个例子,我粘贴了这个代码(只是一个例子)。 我可以在这个上使用静态或非静态

"static const" vs "#define" vs "enum"

Which one is better to use among the below statements in C? static const int var = 5; or #define var 5 or enum { var = 5 }; Generally speaking: static const Because it respects scope and is type-safe. The only caveat I could see: if you want the variable to be possibly defined on the command line. There is still an alternative: #ifdef VAR // Very bad name, not long enough, too general

“static const”vs“#define”vs“enum”

在C中的下面的语句中使用哪一个更好? static const int var = 5; 要么 #define var 5 要么 enum { var = 5 }; 一般来说: static const 因为它尊重范围并且是类型安全的。 唯一的警告我可以看到:如果你想要的变量可能在命令行定义。 还有一个选择: #ifdef VAR // Very bad name, not long enough, too general, etc.. static int const var = VAR; #else static int const var = 5; // default value #endif

in function to repeat string or char in .net?

Is there a function in c# that returns x times of given char or string. Or I must code it? string.Join("", Enumerable.Repeat("ab", 2)); Returns "abab" And string.Join("", Enumerable.Repeat('a', 2)) Returns "aa" string.Concat(Enumerable.Repeat("ab", 2)); returns "abab" For strings you should indeed use Kirk's solution: string.Join("", Enumerable.Repeat("ab", 2)); Howev

在函数重复字符串或字符在.NET中?

在c#中有一个函数返回给定字符或字符串的x倍。 或者我必须编码? string.Join("", Enumerable.Repeat("ab", 2)); 返回 "abab" 和 string.Join("", Enumerable.Repeat('a', 2)) 返回 "aa" string.Concat(Enumerable.Repeat("ab", 2)); 回报 “ABAB” 对于字符串,你应该确实使用Kirk的解决方案: string.Join("", Enumerable.Repeat("ab", 2)); 然而,对于字符,你可以使用内建的(更高效的)字符串构造函数: new