Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Sprawl
Nov 21, 2005


I'm a huge retarded sperglord who can't spell, but Starfleet Dental would still take me and I love them for it!

Smugdog Millionaire posted:

I've been surveying people about this C# behavior: http://dotnetpad.net/ViewPaste/zgKo84NzE0-wEQs8fYvVkA

About 50% don't find it surprising and about 50% do. What's kind of interesting to me is that the people who don't find it surprising don't have a clear picture of why it does what it does. The people who do find it surprising have a clearer picture of what they want it to do, but they're wrong of course. Also, there seems to be a correlation where the programmers I perceive as being smarter or more knowledgeable fall into the 'surprised' camp.

I've only surveyed a handful of people though.

That does exactly what i would expect it to be doing.

Adbot
ADBOT LOVES YOU

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Smugdog Millionaire posted:

I've been surveying people about this C# behavior: http://dotnetpad.net/ViewPaste/zgKo84NzE0-wEQs8fYvVkA

About 50% don't find it surprising and about 50% do. What's kind of interesting to me is that the people who don't find it surprising don't have a clear picture of why it does what it does. The people who do find it surprising have a clearer picture of what they want it to do, but they're wrong of course. Also, there seems to be a correlation where the programmers I perceive as being smarter or more knowledgeable fall into the 'surprised' camp.

I've only surveyed a handful of people though.
I don't know that I'm surprised, exactly. It's not what I'd naively expect, but I read Fabulous Adventures in Coding often enough to know that this sort of thing can often act in ways contrary to a naive understanding. It makes sense to me why it happens that way, though I haven't yet pinned down the exact rules that cause this behavior. Ultimately it just goes to show that generics aren't templates.

EDIT: And here's how to make it behave the way I might have originally expected: http://dotnetpad.net/ViewPaste/EtPyvevTwUaRKCRRMZAfIg though I hope you don't mind some run-time resolution.

Zhentar posted:

If you read it just a little more often, you wouldn't be surprised at all.
I have indeed read that exact post. I just filed it away under "remember this when you start playing around with things like that."

Jethro fucked around with this message at 20:32 on Oct 27, 2011

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
I find that unsurprising. The only details you need to know for it to make sense are that unlike C++ templates, C# generics are only compiled to one copy of the function and that overload resolution is done at compile time.

Zhentar
Sep 28, 2003

Brilliant Master Genius

Jethro posted:

It's not what I'd naively expect, but I read Fabulous Adventures in Coding often enough to know that this sort of thing can often act in ways contrary to a naive understanding.

If you read it just a little more often, you wouldn't be surprised at all.

boo_radley
Dec 30, 2005

Politeness costs nothing
"Generics are not templates" is a great summary.

Geno
Apr 26, 2004
STUPID
DICK
My company uses Microsoft Exchange server and for my ASP.NET website, I am trying to retrieve the Public Distribution Lists (PDLs) for what some users are in. Is there any way to do this?

wwb
Aug 17, 2004

What version of exchange? Pray it is 2007 or better and you get exchange web services.

Geno
Apr 26, 2004
STUPID
DICK

wwb posted:

What version of exchange? Pray it is 2007 or better and you get exchange web services.

Yeah, it's 2010.

Nurbs
Aug 31, 2001

Three fries short of a happy meal...Whacko!
I have a function that essentially does an object -> csv transformation. What I'd like to do though, is pass in an array of accessors for the properties instead of reflecting over names etc.

What I want is to do something like

ObjectToCSV(i => i.PropertyA, i.PropertyB, i.PropertyC.subPropertyA) and be able to tell in the function what the full property name is (e.g. PropertyA, PropertyB, PropertyC.subPropertyA) and its value.

Sedro
Dec 31, 2008
You're looking for LINQ expressions. Your method signature will look something like this:
code:
static string ObjectToCSV<TObject>(TObject obj, params Expression<Func<TObject, object>> propertyExpressions);

// usage:
Foo myFoo = ...;
string csv = ObjectToCSV<Foo>(
    myFoo
    foo => foo.PropertyA,
    foo => foo.PropertyB,
    foo => foo.PropertyC.subPropertyA);
You'll be able to grab the PropertyInfo from the Expression. You're still doing reflection so you may want to create an object which can cache the bulk of the reflection work.
code:
interface IObjectToCSVConverter<TObject>
{
    IObjectToCSVConverter<TObject> RegisterProperty<TProperty>(Expression<Func<TObject, TProperty>> propertyExpression);
    string ToCSV(TObject obj);
}

// setup:
static readonly IObjectToCSVConverter Converter = new ObjectToCSVConverter<Foo>()
    .RegisterProperty(foo => foo.PropertyA)
    .RegisterProperty(foo => foo.PropertyB)
    .RegisterProperty(foo => foo.PropertyC.subPropertyA);

// usage:
Foo myFoo = ...;
string csv = Converter.ToCSV(myFoo);

Dietrich
Sep 11, 2001

Here's the basics of reflecting a lambda.

code:
public string PropertyName (Func<object> property);
{
  var lambda = (LambdaExpression)property;
  MemberExpression memberExpression;
  if (lambda.Body is UnaryExpression)
  {
    var unaryExpression = (UnaryExpression)lambda.Body;
    memberExpression = (MemberExpression)unaryExpression.Operand;
  }
  else memberExpression = (MemberExpression)lambda.Body;

  return memberExpression.Member.Name
}

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Nurbs posted:

I have a function that essentially does an object -> csv transformation. What I'd like to do though, is pass in an array of accessors for the properties instead of reflecting over names etc.

What I want is to do something like

ObjectToCSV(i => i.PropertyA, i.PropertyB, i.PropertyC.subPropertyA) and be able to tell in the function what the full property name is (e.g. PropertyA, PropertyB, PropertyC.subPropertyA) and its value.

You could use this extension to String to do what you want like this:

code:
somethingInstance.a = "Your"
somethingInstance.c = "mother"
"\"{a}\",\"{c}\",...".FormatWith(somethingInstance)
The accessors are basically stored in the format string in this case.

Or I suppose the extension method could be altered to do CSV formatting

ljw1004
Jan 18, 2005

rum

Nurbs posted:

I have a function that essentially does an object -> csv transformation. What I'd like to do though, is pass in an array of accessors for the properties instead of reflecting over names etc.

Are you re-inventing serialization?

Eggnogium
Jun 1, 2010

Never give an inch! Hnnnghhhhhh!
I have a static class in which I want to have some "constants" for other classes to use. Problem is, some of these constants can't actually use const keyword (e.g. one is a TimeSpan object, another is a string pulled from an environment variable).

My solution is to make them properties with a private set that get initialized in the static constructor. I just wondered if there's a cleaner way to do this?

Edit: Actually, it looks like readonly might be the keyword I need. Is that right?


VVV Thanks, just found a page just like that :doh:

Eggnogium fucked around with this message at 20:20 on Oct 28, 2011

Milotic
Mar 4, 2009

9CL apologist
Slippery Tilde

Eggnogium posted:

I have a static class in which I want to have some "constants" for other classes to use. Problem is, some of these constants can't actually use const keyword (e.g. one is a TimeSpan object, another is a string pulled from an environment variable).

My solution is to make them properties with a private set that get initialized in the static constructor. I just wondered if there's a cleaner way to do this?

The readonly keyword is your friend here:

http://weblogs.asp.net/psteele/archive/2004/01/27/63416.aspx

Dietrich
Sep 11, 2001

A constant can only be a primitive because all references to it in code are literally replaced with the declared value on compilation. A read-only variable, on the other hand, can be anything, but does not get the slight performance edge of avoiding a reference lookup on execution.

Eggnogium
Jun 1, 2010

Never give an inch! Hnnnghhhhhh!

Dietrich posted:

A constant can only be a primitive because all references to it in code are literally replaced with the declared value on compilation. A read-only variable, on the other hand, can be anything, but does not get the slight performance edge of avoiding a reference lookup on execution.

This doesn't actually impact me but out of curiosity, are readonly fields populated before the constructor is called? Like if I needed to do a few lines of processing before having everything set up to instantiate my read only field, would I be able to do that and assign to the field in the constructor? Or does the initialization have to be in-line in the class defintion?

gariig
Dec 31, 2004
Beaten into submission by my fiance
Pillbug
You can set ReadOnly variables in the constructor.

Dietrich
Sep 11, 2001

Eggnogium posted:

This doesn't actually impact me but out of curiosity, are readonly fields populated before the constructor is called? Like if I needed to do a few lines of processing before having everything set up to instantiate my read only field, would I be able to do that and assign to the field in the constructor? Or does the initialization have to be in-line in the class defintion?

Read only fields can only be populated inline or during the constructor. It is good practice to make fields that are initialized via the constructor readonly unless you have need for them to be changed post construction. In fact it is one of the suggestions offered by resharper by default.

Nurbs
Aug 31, 2001

Three fries short of a happy meal...Whacko!

ljw1004 posted:

Are you re-inventing serialization?

God no, that's not what I want to do. I was just looking for a way to de-serialize an object to csv with linq expressions representing X properties instead of string names of properties.

Thanks to all the repliers!

Nurbs fucked around with this message at 05:59 on Oct 29, 2011

Dr Monkeysee
Oct 11, 2002

just a fox like a hundred thousand others
Nap Ghost

Eggnogium posted:

This doesn't actually impact me but out of curiosity, are readonly fields populated before the constructor is called? Like if I needed to do a few lines of processing before having everything set up to instantiate my read only field, would I be able to do that and assign to the field in the constructor? Or does the initialization have to be in-line in the class defintion?

Inline intialization is syntactic sugar for constructor code anyway. If you look at the actual IL any in-line field initialization statements are just copied into the top of all constructors defined on the class.

NtotheTC
Dec 31, 2007


I'm using connector/net to get a datatable object of a mysql table, and one of the columns in the mysql table is longtext. I'm trying to bind the column to a control, but the textbox/richtextbox controls don't seem capable of handling it, is there a different control I should be using?

Edit: nevermind i'm a complete moron that doesn't use enough exception handling.

NtotheTC fucked around with this message at 21:10 on Oct 30, 2011

raminasi
Jan 25, 2005

a last drink with no ice
I have a C application that receives a FILE * and writes stuff to it, and I want to use this functionality from .NET (4.0). I know how to do basic platform invoke, but I can't figure out what to use for the FILE * in the call into the native application. I tried using the unsafe handle from an anonymous PipeStream but I'm not receiving any data (even after I turned buffering off on the C side - I couldn't see how to try that on the C# side). I feel like I should be using an UnmanagedMemoryStream but I can't figure out how to set it up.

As icing on the cake, this needs to be asynchronous.

Sedro
Dec 31, 2008
The managed APIs wrap HANDLE which is not the same as FILE*. You will have to call a native method which returns a FILE*, like fopen. You might be able to use Windows APIs like _fdopen to get a FILE* from a HANDLE.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

GrumpyDoctor posted:

I have a C application that receives a FILE * and writes stuff to it, and I want to use this functionality from .NET (4.0). I know how to do basic platform invoke, but I can't figure out what to use for the FILE * in the call into the native application. I tried using the unsafe handle from an anonymous PipeStream but I'm not receiving any data (even after I turned buffering off on the C side - I couldn't see how to try that on the C# side). I feel like I should be using an UnmanagedMemoryStream but I can't figure out how to set it up.

As icing on the cake, this needs to be asynchronous.

Any reason why you can't just run a completely separate C# application that reads the file?

raminasi
Jan 25, 2005

a last drink with no ice

Sedro posted:

The managed APIs wrap HANDLE which is not the same as FILE*. You will have to call a native method which returns a FILE*, like fopen. You might be able to use Windows APIs like _fdopen to get a FILE* from a HANDLE.

Ah, thanks. Time to do some digging I guess. (I found a stackoverflow question asking about the reverse problem.

Ithaqua posted:

Any reason why you can't just run a completely separate C# application that reads the file?

There are no actual files (necessarily) involved.

Sedro
Dec 31, 2008

GrumpyDoctor posted:

There are no actual files (necessarily) involved.

You should consider involving them unless you have a compelling reason not to. Using FILE* across a managed->native boundary is going to be more headache than it's worth.

raminasi
Jan 25, 2005

a last drink with no ice

Sedro posted:

You should consider involving them unless you have a compelling reason not to. Using FILE* across a managed->native boundary is going to be more headache than it's worth.

No, I mean there are literally no files to involve. The problem at the root of all this is that I have a native DLL that writes to standard output (Pretty Dumb, I know, but this is where poor planning gets you) and I want to capture that output. If there's a better way to do this, I'm all ears; I was under the impression that DLLs didn't have their own standard outputs, and since my hosting application uses a GUI it didn't have a standard output to redirect either, so I just quickly modified the DLL to write an an arbitrary FILE *, hoping that I could pass something useful to it. If that isn't the case there are definitely some other things I can try.

TasteMyHouse
Dec 21, 2006
You could wrap the dll in a console application, then invoke it as a subprocess (disabling its console window) and capture the stdout of that.

TasteMyHouse fucked around with this message at 13:52 on Oct 31, 2011

raminasi
Jan 25, 2005

a last drink with no ice

TasteMyHouse posted:

You could wrap the dll in a console application, then invoke it as a subprocess (disabling its console window) and capture the stdout of that.

Yeah, that's what I decided to go with for the moment. Unfortunately, it turns out that Windows doesn't let you control buffering if you're redirecting standard output :argh:

keep it down up there!
Jun 22, 2006

How's it goin' eh?

I'm not sure if this would be better placed in the web development questions thread, but here goes.

I have a website in ASP.NET using MVC with a secure login system. What I now need is a good way to give the users file downloads, but obfuscate or limit the file URL so users can't just give the file location away to their friends. (Our site is only available to paid customers)

What would be the best way to do this? I imagine passing a file id to a controller and then having the download popup would be the best route. But I wanted to ask in case I'm overlooking something important.

Dietrich
Sep 11, 2001

When I had to do something like that, I generated a record keyed with a Guid indicating the target file on a downloads table. Their link would go to a page "downloads.aspx?id={guid}", which would then respond with the file contents. When the file finished transferring, download.aspx would update the record to indicate it had been downloaded. I had the logic such that a link would be good like 5 times in case they had issues downloading or misplaced the file. After it had been downloaded that number of times, or if a week had passed since the record had been created, the downloads.aspx would instead redirect them to a page stating that the link was no longer good.

It's a pretty easy problem to solve if you couple it with a database.

Dietrich fucked around with this message at 21:05 on Oct 31, 2011

Sprawl
Nov 21, 2005


I'm a huge retarded sperglord who can't spell, but Starfleet Dental would still take me and I love them for it!

BUGS OF SPRING posted:

I'm not sure if this would be better placed in the web development questions thread, but here goes.

I have a website in ASP.NET using MVC with a secure login system. What I now need is a good way to give the users file downloads, but obfuscate or limit the file URL so users can't just give the file location away to their friends. (Our site is only available to paid customers)

What would be the best way to do this? I imagine passing a file id to a controller and then having the download popup would be the best route. But I wanted to ask in case I'm overlooking something important.

Ah you could always handle the file with session variables or post data.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:
In managed C++, if I take a pin_ptr<> of a class member, does that prevent the entire class from being moved by the garbage collector (as long as the pin_ptr<> is in scope)?

Sedro
Dec 31, 2008

dwazegek posted:

In managed C++, if I take a pin_ptr<> of a class member, does that prevent the entire class from being moved by the garbage collector (as long as the pin_ptr<> is in scope)?

According to msdn

quote:

Pinning a sub-object defined in a managed object has the effect of pinning the entire object.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

Sedro posted:

According to msdn

Ugh, don't read documentation on Friday and then expect to remember all of it on Monday.

Anyway, thanks, looks like I have a few changes to make.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
What are some existing systems in place for exchanging arbitrary objects defined at run-time potentially between threads or even processes using .NET? I'm thinking of something almost like some global Dictionary of string keys to objects as a starter. It would be nice if the object would be updated in the store the moment something in it was changed.

For a starter I was going to serialize a bunch of stuff to a known spot on disk, but I see that getting obnoxious quick, and it also doesn't convey changes until the object gets stored again.

I'd think basically an object-based database or something would work for this, but I am not really familiar with them, let alone getting them up and running with .NET and C# code.

Sedro
Dec 31, 2008
There are huge performance implications at each level of separation (ie. HTTP vs. inter-thread communication). I don't know of anything that abstracts it all away.

Communicating between AppDomains: MAF is the only thing I know of
Communicating between processes: WFC over named pipe
Communicating between physical machines: WCF over TCP, HTTP, SOAP, etc.

There are plenty of ways to communicate between threads. If you want a dictionary, ConcurrentDictionary is a nice thread-safe dictionary with no mutex locking.

ljw1004
Jan 18, 2005

rum

Rocko Bonaparte posted:

What are some existing systems in place for exchanging arbitrary objects defined at run-time potentially between threads or even processes using .NET?. It would be nice if the object would be updated in the store the moment something in it was changed.

Think hard before designing around the bit in bold. The best, cleanest, lowest bug-count, most robust, scalable, performant way to share data across thread/process boundaries is with IMMUTABLE data.

A great way to get into the "immutable" mindset is with the F# language in VS2010. This language guides you into the pit-of-success by making it easier to write immutable data than mutable. You'll get the hang of it within a week of F# coding.

It's possible, by changing your mindset, to forge ahead with immutability even in the unlikeliest places. For instance, you'd think that console/UI input and output is the most mutating thing there is -- but the Haskell language manages to do them in a completely immutable way!

Let the mutation happen only at rare well-defined boundaries, e.g. you can expect the records in a database to mutate. But don't let that infect much of your own code.

The C#/VB team had to wrestle with how to represent the parse-tree of source code in the editor window to plugins. The problem is that the parse-tree can be modified by the user, and the plug-ins run in different threads or processes. The solution we used was to give each plugin an immutable snapshot of the tree at a given moment in time. (Obviously, when the plugin wants to call an API to update the source code, it needs to check whether its snapshot is still current.)

Adbot
ADBOT LOVES YOU

Nurbs
Aug 31, 2001

Three fries short of a happy meal...Whacko!
I need a sounding board for an idea I had.

I'm doing some SalesForce querying, and the result set, if larger than the batch size will be chunked by salesforce into batches, if there are more results than in the current set, it gives you a locator to start the next.

What I was thinking was if I could create a collection that served as the front end to a collection of batches, iterating through each one as it was completed, instead of blocking the entire result set until it was sucked down.

The idea would be to return an IEnumerable<T> that was really an IDictionary<int, TBatch> , where TBatch would be

code:
class Batch<T>
{
bool IsFinished { get; set; }
ICollection<T> Results { get; set; }
}
The idea being that the querying code can start a thread that adds a new batch item to the Dictionary look for a locator, and if its found, start a new thread to add the current items to the batch and query for the next batch, essentially loading all batches in parallel. Even as the Dictionary is being loaded, it could be returned and iterated over by an Enumerator which iterates through each batch thats marked as finished.

Overly complicated? Already exists as something and I don't know it? Terrible idea that won't be the gain I think it is? I'm all ears.

  • Locked thread