是否有可能在应用程序启动时"自动"初始化静态类?我自动意思是不需要引用属性.
我希望能够做到这一点的原因是我想在启动时自动设置应用程序的主题.
这是一个简短的片段:
static class Settings{
private static Theme _defaultTheme;
public static Theme DefaultTheme{
get{
return _defaultTheme;
}
private set{
_defaultTheme = value;
ThemeManager.SetTheme(value);
}
}
static Settings(){
DefaultTheme = Themes.SomeTheme;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以(现在就是这样)和原来的getter/setter一起去打电话
ThemeManager.SetTheme( Settings.DefaultTheme );
Run Code Online (Sandbox Code Playgroud)
在App的构造函数(它是WPF项目)中它会完成这项工作,但是,至少从我的观点来看(如果我错了请纠正我),更适合默认主题应用而不是需要明确说明它.
c# static-constructor static-classes static-initialization static-class
我需要在静态类中获取或访问我的IoC容器.这是我的(简化)场景:
我在一个Startup类中注册ASP .net Web Api的依赖项(但我也是为MVC或WCF注册的.我有一个DependecyResolver项目,但为了简单起见,请考虑以下代码)
// Web Api project - Startup.cs
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
var builder = new ContainerBuilder();
// ... Omited for clarity
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.AsClosedTypesOf(typeof(IHandle<>))
.AsImplementedInterfaces();
// ...
IContainer container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// ...
}
Run Code Online (Sandbox Code Playgroud)
然后,在一个单独的类库中,我有我的静态类(为了清晰起见,再次简化):
public static class DomainEvents
{
private static IContainer Container { get; set; }
static DomainEvents()
{
//Container = I need get my Autofac container here
}
public static void Register<T>(Action<T> callback) where …Run Code Online (Sandbox Code Playgroud) 我在C#AppDomain遇到了一个大问题.
我需要在.dll文件中加载一个静态类并执行它的方法:
当我尝试加载它们时
Assembly.LoadFrom("XXXXX") // (XXXXX is the full path of dll)
Run Code Online (Sandbox Code Playgroud)
.dll不会自动或以编程方式卸载.
当我尝试在AppDomain中加载它们时
adapterDomain = AppDomain.CreateDomain("AdapterDomain");
(a)adapterDomain.CreateInstanceFrom(this.AdapterFilePath, this.AdapterFullName);
(b)adapterAssembly=adapterDomain.Load(AssemblyName.GetAssemblyName(this.AdapterFilePath));
Run Code Online (Sandbox Code Playgroud)
如果我使用方法(a),因为目标类是静态类,它不起作用.
如果我使用方法(b),因为目标.dll与我的项目不是同一个目录,我将得到一个例外.
如何加载.dll和静态类,然后在使用后卸载.dll?
静态内部类是否有性能成本?或者我应该只编写与非内部类相同的静态类?
我是android的新手,我尝试开发一个系统但是当我完成代码时,处理程序会显示此警告
下面显示我编辑后的代码,事件ontounch中的处理程序显示警告处理程序无法解析.我尝试将//忽略处理程序,我尝试运行应用程序并将其结果强制关闭.
public class MainActivity extends Activity {
protected static final int STOP = 100;
ImageView iv;
private ProgressBar pb;
LinearLayout ll;
private AnimationDrawable anim;
ScrollView sv;
private SQLiteDatabase db;
private boolean flagscanning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = new LinearLayout(this);
new HandlerClass(this);
db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory()+"/antivirus.sqlite", null, SQLiteDatabase.OPEN_READONLY);
iv = (ImageView) this.findViewById(R.id.imageView1);
//???????
pb = (ProgressBar) this.findViewById(R.id.progressBar1);
ll = (LinearLayout) this.findViewById(R.id.ll);
//??ImageView?????????
iv.setBackgroundResource(R.drawable.bg);
//sv???????????
sv = (ScrollView) this.findViewById(R.id.scrollView1);
anim = (AnimationDrawable) iv.getBackground();
}
private …Run Code Online (Sandbox Code Playgroud) 我正在研究Trie的数据结构并遇到了这段代码
// R-way trie node
private static class Node {
private Object val;
private Node[] next = new Node[26];
}
Run Code Online (Sandbox Code Playgroud)
我理解逻辑,但我没有得到的是,Node将被初始化的深度是多少?
您可以在http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/TrieST.java.html查看完整的代码.
我正在寻找一个处理MVC3/.Net应用程序的数据库访问的类.
该类是静态的,为常见的数据库查询提供了很好的方便方法 - 各种各样的东西,如"GetColumnBValueForColumnA()",以及更复杂的查询.对于给定的解决方案和域,它是一个很好的因子/优化因素.
然而,看到这个类是静态的,引发了一些关于这可能是一个坏主意的遗忘的记忆(也许是在多线程的背景下?),我无法摆脱这种感觉.
保持这种类静态是一个好主意,还是应该为每个数据库调用实例化它?
我有一个棘手的问题,一直困扰着我.我有以下代码声明......
namespace ESEGURCI.WEB.BusinessLogicLayer.Commons
{
public static class ParameterUtilities
{
public enum ParameterEnum
{
MAX_LOGIN_ATTEMPTS,
AUDIT_MODIFICATIONS
}
}
}
Run Code Online (Sandbox Code Playgroud)
我调用这样的代码" ParameterUtilities.ParameterEnum.MAX_LOGIN_ATTEMPTS "问题是每次满月时我得到错误"对象引用没有设置为对象的实例"这一行......就像代码只能工作99.9%的时间...
我发现的唯一事情是,因为枚举是一个值类型,当调用静态类时,枚举可能有空......但我找不到有关此行为的任何文档...
有人可以告诉我为什么会这样吗?我知道我应该从静态类中删除枚举,并将枚举声明为独立但我想知道为什么会发生这种情况......
谢谢,S
更新
好的,对于要求更多代码的每个人,以下是发生错误的完整功能...
public static int GetPageSize(int companyId)
{
int pageSize = 0;
// error happens bellow this line
ESEGURCI.WEB.BusinessLogicLayer.Entities.Parameter parameter = ESEGURCI.WEB.BusinessLogicLayer.Entities.Parameter.GetParameter(ParameterUtilities.ParameterEnum.AUDIT_MODIFICATIONS.ToString(), companyId);
// error happens above this line
int.TryParse(parameter.Value, out pageSize);
return pageSize;
}
Run Code Online (Sandbox Code Playgroud) 我知道这取决于编写匿名类的上下文(静态或非静态方法)。但看看这部分代码:
public class A {
int fieldOfA;
private static class B {
int fieldOfB;
}
public static void main(String[] args) {
B obj = new B() { //this anonymous class is static becuase is in the main method.
private static void testMethod() { //so why here i have an error and i can put just a non-static method
//if my class is static ?
//a class static can have static method, but this class no, why?
}
};
}
} …Run Code Online (Sandbox Code Playgroud) 我是OOP的新手,我想我不懂静态类.
我想创建一个静态类Actions和一个用于更改textblock外观的静态方法.
这是我的代码:
public static class Tools
{
public enum StatusOption
{
Online,
Offline,
Warning
}
}
public class Actions
{
private SortedDictionary<Tools.StatusOption,SolidColorBrush> StatusColors = new SortedDictionary<Tools.StatusOption,SolidColorBrush>();
public Actions()
{
StatusColors.Add(Tools.StatusOption.Online, new SolidColorBrush(Colors.Green));
StatusColors.Add(Tools.StatusOption.Offline, new SolidColorBrush(Colors.Red));
StatusColors.Add(Tools.StatusOption.Warning, new SolidColorBrush(Colors.Orange));
}
public void SetStatus(Tools.StatusOption _statusOption, TextBlock _txtBlock)
{
_txtBlock.Text = _statusOption.ToString();
_txtBlock.Foreground = StatusColors[_statusOption];
}
}
Run Code Online (Sandbox Code Playgroud)
它工作,但我必须创建我的类的几个实例,这是恕我直言没用.
private void Close_Click(object sender, RoutedEventArgs e)
{
Actions a1 = new Actions();
a1.SetStatus(Tools.StatusOption.Offline, StatusTextBlock);
}
private void Open_Click(object sender, RoutedEventArgs e)
{
Actions a2 …Run Code Online (Sandbox Code Playgroud) 我有一个(现在)是静态的类:
public static class Grob
{
public static void Frob()
{
Foo.Bar();
}
}
Run Code Online (Sandbox Code Playgroud)
这很有效.代码电话:
Grob.Frob();
Run Code Online (Sandbox Code Playgroud)
一切都与世界是对的.现在我希望我的类实现一个接口:
public static class Grob : IOldNewGrob
{
public static void Frob()
{
Foo.Bar();
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用,因为原因.
所以我会尝试更改为单个类:
public sealed class Grob
{
private static volatile Singleton instance;
private static object syncRoot = new Object();
private Grob() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance …Run Code Online (Sandbox Code Playgroud) static-class ×11
c# ×7
java ×2
.net ×1
android ×1
appdomain ×1
autofac ×1
constructor ×1
enums ×1
memory-leaks ×1
oop ×1
performance ×1
singleton ×1
trie ×1