Category: Misc
Passing property by reference (ref) in C#
In C# it is impossible to pass a property by reference.
This is due to the fact that properties are actually methods in disguise.
They behave like members (which are real objects), but are methods in nature.
Probably, this is done to enhance memory allocation issues for objects in C#.
Whatever the reason is, I faced the problem with passing those properties into the function so that the function alters them.
Initially I tried to do some workarounds using delegates (pointers to the methods).
However, they were not flexible enough to meet my needs.
Here is the final solution, which is based on the Microsoft’s “reflection” library.
public static void assignValueByRef(object obj, string propertyName, object src)
{
PropertyInfo propInfo = obj.GetType().GetProperty(propertyName);
//if “property to set” is INT
if (propInfo.GetValue(obj, null).GetType() == typeof(int))
{
if (src.GetType() == typeof(TextBox))
{
if (((TextBox)src).Text.Trim() != “”)
{
propInfo.SetValue(obj, Convert.ToInt32(((TextBox)src).Text.Trim()), null);
}
}
else if (src.GetType() == typeof(DropDownList))
{
if (((DropDownList)src).SelectedValue.ToString() != “”)
{
propInfo.SetValue(obj, Convert.ToInt32(((DropDownList)src).SelectedValue.ToString()), null);
}
}
}
}
We call the method as follows:
Utilities.assignValueX(objJF,
“PloshadObshaia”,
bridgeControl.FindControl(“FormView1”).FindControl(“dataPloshadObshaia”));
where “PloshadObshaia” is the name of the property of objJF object.
How to pass by reference in C#
1. Calling statement should be like:
callingFunc(ref x, ref y)
2. Definition statement should be like:
public void callingFunc(ref int x, ref string y)
NOTE: Classes and objects are passed by reference by default in C#.
error: Configuration system failed to initialize
configSections should be directly after <configuration>
<configuration>
<configSections></configSections>
Multi-language forms (dynamic without form reload and reinitialization)
Memory allocation and overflows outlined
Program can store its data in 3 places:
– data area (contains only static variables)
– stack (contains local variables and return addresses when subroutine is called)
– heap (dynamically allocated memory, objects are allocated here due to their size)
Buffer overflow:
– Stack overflow
Limited amount of memory (usually determined when program starts)
Usually caused by 2 errors:
– infinite recursion
– very large stack variables (usually creating too large local variables, therefore arrays larger than a few KB should be allocated dynamically)
ex:
int foo() {
double x[1000000];
}
* when a single-threaded program runs as a multi-threaded, it will be allocated less space per thread. Therefore program that runs OK might crush when multi-threaded.
– Heap overflow
Memory is allocated dynamically
Primarily program DATA is contained
* on average, half of all critical security leaks are based on heap overflows (ex: iPhone, PS3 homebrews)
* more information: http://www.h-online.com/security/features/A-Heap-of-Risk-747161.html
How to set proxy on Vista / Win7
To set proxy
1. run cmd as administrator (find it in the start menu, right click and run as admin.)
2. netsh
3. winhttp
4. set proxy myproxy.address.here:8080
To unset proxy
netsh
winhttp
reset proxy
For help
netsh
winhttp
help
To see proxy settings
netsh
winhttp
show proxy
Metabase problem in IIS when using .NET 2 Framework
In the command line execute the following command:
aspnet_regiis -ga MACHINE_NAME\IIS_PROCESS_ACCOUNT
If you don’t know which account is IIS_PROCESS_ACCOUNT do the following:
aspnet_regiis -ga everyone
On non-english systems, “everyone” should be replaced with a respective word.
Automating Office Applications on the Server Side
If the aim of the automation is to use a predefined template, then it is not a good idea.
Read article from microsoft:
http://support.microsoft.com/kb/257757/en-us
Hence, ASP.NET code used on the server side sometimes fails to execute due to internal “Interop” reasons.
Also, important to note that during debugging code works OK.
The problem arises only when website is published on the IIS.
Same occurs for both Vista and Windows Server 2003.
The workaround is to call EXE windows forms application from ASP.NET code and then use the file generated by EXE.
How to register COM DLL on the server
Issue:
When I was trying to run the application that used the reference, error occurred:
“System.Runtime.InteropServices.COMException (0x80040154). Retrieving the COM class factory for component xxxx failed due to the following error: 80040154”
1. The COM component needs to be registered on his laptop. Start + Run, regsvr32 c:\wavetracker\componentname.dll
DID NOT HELP, another error:
The module XXX was loaded but entry-point DllRegisterServer was not found.
Make sure that XXX is a valid DLL or OCX file and then try again.
2. http://technet.microsoft.com/en-us/sysinternals/0e18b180-9b7a-4c49-8120-c47c5a693683.aspx
download:
– Process Explorer (a GUI-based version of ListDLLs from sysinternals)
– Process Monitor (fully replaces “RegMon”(another suite from sysinternals) on windows vista )
3. Usually the root of the problem is difficult to identify.
In my case, the problem was the wrong usage of classes and functions of the library on hand.
Hence, sometimes when you use library functions/classes in the wrong way it might give this error.
So, this error NOT ALWAYS means the lack of DLL or its registration!!!