控制台应用程序调用我的类库时出错 [英] Error in Console app to call my class lib

查看:88
本文介绍了控制台应用程序调用我的类库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我编写了一个类文件,并通过控制台应用程序调用该类文件.

我的控制台应用程序出现错误

无法将类型"ActiveDir.employee"隐式转换为字符串"

控制台应用程序代码:

Hi,

I wrote a class file and calling the class file thru my console application.

I got an error on my console application

"can''t implictly convert type ''ActiveDir.employee to string"

Console Application code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace activeDirectoryLdapExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter user: ");
            String username = Console.ReadLine();
            ActiveDir.ActiveDirSearch search = new ActiveDir.ActiveDirSearch();
            string output = search.SearchUser(username);

        }

    }
}




类文件代码:




Class File Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ActiveDir
{
    public class Employee
{
    // instance variables 
     public String name;
    public String surname;
     public string title;
        //   ...
    // Constructors
    public Employee()
    {
    }
}

    public class ActiveDirSearch
    {
        public ActiveDirSearch()
        { }

        public Employee SearchUser(string username)
        {
            
            Employee employee = new Employee();

            if (username.Contains("."))
            {
                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];
            }


            try
            {
                DirectoryEntry entry = new DirectoryEntry("GC://Test", "Test\\Test", "Ld@Test", AuthenticationTypes.Secure);

                // DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);
                //  search.Filter = "(cn=" + username + ")";

                if (username.IndexOf(" ") > 0)
                    search.Filter = "(cn=" + username + ")";
                else
                    search.Filter = "(sn=" + username + ")";

                SearchResult result = search.FindOne();


                if (result != null)
                {

                    // user exists, cycle through LDAP fields (cn, telephonenumber etc.)

                    ResultPropertyCollection fields = result.Properties;

                    foreach (String ldapField in fields.PropertyNames)
                    {
                        // cycle through objects in each field e.g. group membership
                        // (for many fields there will only be one object such as name)                        
                        foreach (Object myCollection in fields[ldapField])
{
     if (ldapField == "name") 
         employee.name = myCollection.ToString();
     if (ldapField == "surname")
         employee.surname = myCollection.ToString();
    if(ldapField=="title")
        employee.title=myCollection.ToString();
    
}
                    }

                }

                else
                {
                   return null;
               }
            }

            catch (Exception e)
            {
                throw e;
            }

            return employee ;
        }
    }
}



我在这里错过了什么吗?



did i miss something here ?

推荐答案

ActiveDir.Employee是您自己的类,那么这是什么问题?它肯定不是字符串,也不会隐式转换为字符串.

您可能想在类Employee的声明中覆盖object.ToString,并在需要字符串的地方调用它.另一种方法是为此类引入隐式String运算符.
像这样的东西:
The class ActiveDir.Employee is your own class, so what''s the problem? It is certainly not a string and not converted to string implicitly.

You may want to override object.ToString in your declaration of the class Employee and call it where string is required. Another way is to introduce implicit String operator for this class.

Something like that:
class Employee {
    public override string ToString() {
        return string.Format("{0} {1} {2}", title, name, surname).Trim();
    }
    public static implicit operator String(Employee value) {
        return value.ToString();
    }
}



我使用string.Trim处理title为空等情况;您可以根据自己的喜好进行改进.

—SA



I used string.Trim to handle the cases if title is empty, etc.; you can improve it do your liking.

—SA


public Employee SearchUser(string username);是您的SearchUser方法的签名.

它返回一个Employee对象,我确定您知道它不是字符串,因此无法分配给字符串.

您应该做的是在Employee类中创建一个方法或属性,该方法或属性以所需的格式返回字符串.
例如
Hi public Employee SearchUser(string username); is the signature of your SearchUser method.

It returns an Employee object, which I''m sure you are aware is not a string, and therefore cannot be assigned to a string.

What you should do is create a method or property within the Employee class that returns a string in the format you want.
e.g.
public class Employee {
  // instance variables
  public String name;
  public String surname;
  public string title;

  public String FullName() {
    return String.Format("{0}. {1} {2}", title, name, surname);
  }
}


然后


and then

string output = search.SearchUser(username).FullName();



艾伦.



Alan.


这篇关于控制台应用程序调用我的类库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆