小编Sea*_* C.的帖子

AttributeUsage中的多个AttributeTargets

[AttributeUsage(AttributeTargets.Property)]
public class MyAttribute : Attribute
{
...
}
Run Code Online (Sandbox Code Playgroud)

我想在同时使用这个自定义属性的属性的Fileds而不是其他.如何分配多个目标(AttributeTargets.PropertyAttributeTargets.Field)?或者这是不可能的?

AttributeTargets.All这不是我想要的.

c# reflection attributes

11
推荐指数
1
解决办法
4953
查看次数

WPF中的ConfigurationManager

我在wpf项目中有一个配置文件来存储connectionstring.但是当我尝试获取AppSettings和ConnectionStrings时,我得到null.

WEB.config文件是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Trackboard" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
  </connectionStrings>
  <appSettings>
    <add key="Trackboard" value="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我尝试过几种方式:

W1: ConnStr = ConfigurationManager.ConnectionStrings["Trackboard"].ConnectionString;
W2: ConnStr = ConfigurationManager.ConnectionStrings[0].ConnectionString;
W3: ConnStr = ConfigurationManager.AppSettings["Trackboard"];
W4: ConnStr = ConfigurationManager.AppSettings[0];
Run Code Online (Sandbox Code Playgroud)

他们都没有工作.

但这个工作:

ConnStr = @"Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf";
Run Code Online (Sandbox Code Playgroud)

(这意味着我不能使用配置文件,这违背我的意愿)我需要帮助.

c# sql wpf configurationmanager appsettings

9
推荐指数
2
解决办法
3万
查看次数

正则表达式分裂围绕卷曲括号

我有一个字符串:

"abc{d}efg{hi}{jk}lm{n}"
Run Code Online (Sandbox Code Playgroud)

我希望它分为:

"abc","{d}","efg","{hi}","{jk}","lm","{n}"
Run Code Online (Sandbox Code Playgroud)

我使用这种模式[{}],结果是"abc","d","efg","hi","","jk","lm","n"

如何保持'{''}'在那里?我如何取出空""之间'}''{'

c# regex string split

5
推荐指数
1
解决办法
1381
查看次数

通用类型继承

public class BaseGenericType<T>
{
}

public class SubGenericType<T>: BaseGenericType<List<T>>
{
}
Run Code Online (Sandbox Code Playgroud)

我上面有两个泛型类型,一个继承自另一个但仍然是通用的.我无法弄清楚的奇怪的事情是typeof(SubGenericType<>).IsSubclassOf(typeof(BaseGenericType<>))返回false.并且typeof(SubGenericType<>).IsSubclassOf(typeof(BaseGenericType<List<>>))仍然返回false.我试着GetGenericTypeDefinition()MakeGenericType()GetGenericArguments()检查继承,仍然没有工作.但是typeof(SubGenericType<int>).IsSubclassOf(typeof(BaseGenericType<List<int>>))返回真实.

我想要的是通过反射获取所有类,然后获取继承自传入的泛型类型的特定类.

例如

(1)List<int> - >

(2)获取泛型类型定义==> List<T> - >

(3)make generic ==> BaseGenericType<List<T>> - >

(4)查找子类==> SubGenericType<T>

(5)make generic ==> SubGenericType<int>

在步骤(4)中,我找不到任何东西,尽管我确实有这个SubGenericType<T>.这是为什么?

c# generics reflection inheritance

5
推荐指数
1
解决办法
777
查看次数

c#中的类引用和GC循环

我看到有人用以下样式编写代码:

Dictionary<string,SomeClass> dict = new Dictionary<string,SomeClass>();
...    
dict.Add(key,someClass);    
...    
dict[key] = null;    
dict.Remove(key);
Run Code Online (Sandbox Code Playgroud)

我想知道是否有dict[key] = null;必要.这是否意味着通知GC?但这someClass是其他地方引用的,它是多余的吗?

c# garbage-collection

3
推荐指数
1
解决办法
123
查看次数

如何使用正则表达式模式替换以下字符串

如何使用正则表达式模式替换以下字符串

The result is {A or D} or {Ef or G}.
Run Code Online (Sandbox Code Playgroud)

The result is {AD} or {EfG}.

只需要_or_在括号中删除它.

c# regex string

2
推荐指数
1
解决办法
54
查看次数