-2

I found a similar question, but it was closed and didn't answer this for me.

I have a static class Globals that holds some variables and another Class Stuff that does things. I want Stuff to see if Globals exists and then change some of its variables to the values stored in Globals. I tried

try{
    for (int i = 0; i < Globals.StuffArray.Length; i++)
        StuffArray[i] = Globals.StuffArray[i];
}
catch
{
}

in the constructor for Stuff.

This obviously doesn't work, but the idea was to try to change the variables and to throw an error, that get's ignored, if Globals.StuffArray doesn't exist.

I would still like to know if it's possible to do this, but I'm not solving my problem this way, I added a setter to change the defaults from Globals and that works fine.

update for clarity:

I wanted to make it so that I could leave out Globals and still have Stuff compile.

Update:

In jvascript you can have something like

if (condition1) var x = 0;
if (x) doStuffTo(x);

JavaScript does stuff in the background to make that logical. I wanted to know if C# can do something similar.

To reiterate: I asked because I'm curious if this is possible. Some of the responses seem to have missed the part where I said I'm not going to solve my problem this way, the problem was easily solved by making a property for Globals to change.

Damien
  • 263
  • 1
  • 4
  • 15
  • 7
    At compile time, you should always know whether a class or member exists. What problem are you trying to solve? – SLaks May 04 '15 at 18:36
  • 2
    You mean "check it for null", not "check if it exists"? Your application wouldn't compile if the variable didn't exist. – mason May 04 '15 at 18:36
  • Are you adding these new variables dynamically? I would use a dictionary then you can check if a variable name is in the dictionary instead of an array. – Marko May 04 '15 at 18:37
  • I wanted to make it so that if I have globals defined Stuff will use its values and if Globals isn't defined Stuff will use its own default values. – Damien May 04 '15 at 18:42
  • No, you can't do that. You will always get a compile-time error if `Globals` does not exist. – John Saunders May 04 '15 at 18:42
  • I now read it five times, I still don't understand. – Gerben Rampaart May 04 '15 at 18:42
  • @JohnSaunders thanks, that is exactly what I was asking. – Damien May 04 '15 at 18:43
  • It sounds like this data isn't actually globally applicable data, and as such shouldn't be stored as you're storing it. You should be using a different mechanism entirely to store this data (perhaps a config file that may or may not exist, and a class that can function properly even if the file can't be found, doesn't have sensible values, etc.). – Servy May 04 '15 at 18:45
  • If `Stuff` depends on some sort of data, that data should be explicitly declared as a dependence, by using constructor parameters, properties. Using, say, reflection for that is complex and cumbersome IMHO. – rsenna May 04 '15 at 19:11

2 Answers2

1

Use reflection.

With reflection you can:

  • Load assemblies
  • See what types are inside an assembly.
  • See what fields and properties are inside a type.
  • Read and write their values.
  • And much more...

During runtime fields and types cannot change in a assembly, so I assume you are refering to an assembly which you do not know at compile time. An assembly which you will load during runtime.

Since I do not know exactly what you need and what you have, my example is an approximation:

var asm = Assembly.LoadFile(@"C:\MyDynamicDll.dll");
var type = asm.GetType("Global");
if (type == null)
    // Do something if Global does not exists.
var fieldInfos = type.GetFields(BindingFlags.Static | BindingFlags.Public); 
foreach(var fieldInfo in fieldInfos)
{
    var value = fieldInfo.GetValue(null);
    // Do something with the value.
}

Be warned: Reflection is pretty slow.

* EDIT *

In your updated question you refer to javascript. Javascript is interpreted during runtime. All types and values are evaluated over and over again, which makes the language a bit slower, but more flexible. C# is interpreted during compilation which makes some things, like types, static which makes it all a bit faster. That is the only reason why script-languages can modify their types during runtime. (Of course there are exception.)

Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
  • Thanks, that's exactly what I wanted to know. I use reflection to check child class properties from Stuff so that I didn't need to write 10 odd, very similar and long functions. You mentioned Reflection is slow, is it inefficient for me to do it that way? Should I just write out the 10 individual functions. – Damien May 04 '15 at 18:57
  • It depends. Suppose a regular call (read and write a property) takes 1us, and reflection 1ms... and you only use it 10 times, then the delay would not be noticable. If you call it a million times, then it would be noticable. – Martin Mulder May 05 '15 at 00:02
0

No, you can't do that. You will always get a compile-time error if Globals does not exist.

Also, your try/catch would not have been a best practice. You would have been catching and ignoring more errors than the one you meant to check. Try to keep such blocks as narrow as possible.

John Saunders
  • 160,644
  • 26
  • 247
  • 397