|
If this field is primary key and you're using LINQ to SQL, then check ColumnAttribute (you can view it by opening source file generated by LINQ to SQL engine). There's a property IsPrimaryKey, which may be useful to you.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Basically you can't. You could override the ToString method of Demo to return "ID" though.
|
|
|
|
|
Hi,
Yes, thats what I see, I can't get this information. I hope future version of .NET will allow us to do that.
|
|
|
|
|
You can hope all you want; it ain't gonna happen.
|
|
|
|
|
And what about this:
Demo demo = new Demo();
demo.ID = 1;
demo.Name = "Demo";
foreach ( Fi in demo.GetType.GetFields(BindingFlags.Public | BindingFlags.Instance)) {
Console.WriteLine("--" + Fi.Name);
}
gives you "ID" and "Name".
|
|
|
|
|
And to get the value for ID and Name :
Fi.GetValue(demo);
|
|
|
|
|
You need to use Linq Expression objects to get the name of a property, from the property itself. Here's a simple test program that demonstrates what you have to do:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Consumer c = new Consumer();
c.TestIt();
}
}
public class Demo
{
public int InstanceProperty { get; set; }
public string InstanceField;
static public int TypeField;
static public int TypeProperty { get; set; }
}
public class Consumer
{
public void TestIt()
{
button1_click(this, new EventArgs());
}
private void button1_click(object sender, EventArgs e)
{
Demo myDemo = new Demo { InstanceProperty = 10, InstanceField = "Kab" };
string n1 = GetMemberName(() => myDemo.InstanceField);
string n2 = GetMemberName(() => myDemo.InstanceProperty);
string n3 = GetMemberName(() => Demo.TypeField);
string n4 = GetMemberName(() => Demo.TypeProperty);
string n5 = GetMemberName(() => myDemo);
}
private string GetMemberName<T>(Expression<Func<T>> itemSpecifierExpression)
{
var bodyAsMemberExpression = itemSpecifierExpression.Body as MemberExpression;
if (bodyAsMemberExpression == null)
throw new ArgumentException("itemSpecifierExpression does not specify an object or an instance or type member");
var propInfo = bodyAsMemberExpression.Member as MemberInfo;
return bodyAsMemberExpression.Member.Name;
}
}
} Note - it may be possible to simplify things a bit, but I've not used Expression enough to be sure.
See INotifyPropertyChanging[^] by atverma[^] for the article where I first saw the technique used.
|
|
|
|
|
|
|
|
hello
For WCF host instantiated and opened programmatically - how to relate to binding in app.config? For example, here we get the channel open:
using (ServiceHost host = new ServiceHost(typeof(GridControllerService)))
{
System.ServiceModel.WSDualHttpBinding binding = new WSDualHttpBinding();
binding.OpenTimeout = new TimeSpan(3, 0, 0);
binding.CloseTimeout = new TimeSpan(3, 0, 0);
binding.SendTimeout = new TimeSpan(3, 0, 0);
binding.ReceiveTimeout = new TimeSpan(3, 0, 0);
binding.MaxBufferPoolSize = 5000;
host.AddServiceEndpoint(typeof(IGridControllerService), binding, "wsDualHttpBinding");
host.Open();
But instead of programmatically code up the binding config, we code have relate it to what's already in app.config
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="SomeHttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
transactionFlow="false"
....
In this case, how can I relate "host" instantiated programmatically (yes weird why not do the whole deal in config right? But that's not my question) to "SomeHttpBinding" in config file.
Thanks
|
|
|
|
|
hello!
I have a following string.
string str="flower pot";
In above string i want to replace the position of the character.
eg: "o" replace with its just next character.
my new string is shown like:
"flwoer pto"
thanx
|
|
|
|
|
public void RelocateChar(ref string text, int pos1, int pos2)
{
char[] supportArray = text.ToCharArray();
char supportChar;
supportChar = supportArray[pos2];
supportArray[pos2] = supportArray[pos1];
supportArray[pos1] = supportChar;
text = new string(supportArray);
}
Maybe there are better solutions, but this one should work.
|
|
|
|
|
Have a look through this code and see whether it solves your problem.
private void button1_Click(object sender, EventArgs e)
{
string str = "flower pot";
MessageBox.Show("The source string " + str + System.Environment.NewLine + System.Environment.NewLine + "The Replace Content " + SwapCharacters(str, 'o'));
}
public string SwapCharacters(string strSrc, char chrSrc)
{
string strDst = string.Empty;
try
{
while (strSrc.IndexOf(chrSrc) > -1)
{
strDst += strSrc.Substring(0, strSrc.IndexOf(chrSrc));
strDst += strSrc.Substring(strSrc.IndexOf(chrSrc) + 1, 1);
strDst += chrSrc;
strSrc = strSrc.Substring(strSrc.IndexOf(chrSrc) + 2);
}
strDst += strSrc;
}
catch (Exception ex)
{
throw ex;
}
return strDst;
}
|
|
|
|
|
Hi,
I've a function tht converts IEnumerable To DataTable,
my problem is that one off the columns allowes Nullable
and then i get this exception-
DataSet does not support System.Nullable <>
here is part of my function-
foreach (PropertyInfo pi in pis)<br />
{<br />
<br />
if (dt.Columns.Count == 0)<br />
{<br />
foreach (PropertyInfo pi in pis)<br />
{<br />
dt.Columns.Add(pi.Name, pi.PropertyType);<br />
}<br />
}
i tried to change the function to-
if (dt.Columns.Count == 0)<br />
{<br />
foreach (PropertyInfo pi in pis)<br />
{<br />
Type propType = pi.PropertyType;<br />
if (propType.IsGenericType && propType. == typeof(System.Nullable))<br />
{<br />
propType = Nullable.GetUnderlyingType(propType);<br />
<br />
}<br />
<br />
dt.Columns.Add(pi.Name, pi.PropertyType);<br />
}<br />
}
but i'm still getting the exception:
+ pi {System.Nullable`1[System.DateTime] "myColumnName"} System.Reflection.PropertyInfo {System.Reflection.RuntimePropertyInfo}
What i'm doing wrong?
|
|
|
|
|
treuveni wrote: What i'm doing wrong?
...
dt.Columns.Add(pi.Name, pi.PropertyType);
Guess you wanted to use propType instead of pi.PropertyType ?
|
|
|
|
|
Hi,
I am getting the same error. Did you figure it out? If so, can you please let me know.
Thanks in advance.
Ken
|
|
|
|
|
How can I check SD card is broken?
|
|
|
|
|
Stick it into the SD card slot. Can you read it?
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
|
Hello again everyone
I have a set of bytes which needs to be bit masked, and i want only the result of the bitmask to be displayed on form.
EG
Byte value 3A 0011 1010
Bit Mask 30 0011 0000
AND 0011 0000
result in byte is 30, but i want only the '3' to be displayed. How do i determine how many bits to shift just by looking at the bit mask value?
|
|
|
|
|
Do you mean you only want the first Hex digit? If so, >> 4
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
yep shifting 4 places would work for that example but im working on a bunch of values and the bitmask is a myriad of values (01,02,04,08,10,20,40,80,C0,FE,FB etc). My question is how do u determine how many bits to shift just by looking at the bitmask value
|
|
|
|
|
Just shift right until the low order bit changes to 1, something like:
int count = 0;
while ((mask & 1) == 0)
{
mask = mask >> 1;
count++;
}
It's time for a new signature.
|
|
|
|
|
ah yes your method could work...gonna give it a shot now
|
|
|
|